Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6318655
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: C/C++

2013-06-30 09:30:23

原文地址:java多线程示例 作者:jiangwen127

这个多线程的目的:多线程执行Consumer,每个线程除了terminal变量之外,其他变量都是独立的
方案:
1. 把terminal设置成static变量,这样所有Consumer线程共享它
2. lock变量用来做synchronized同步

注意,每个线程是new出来的,也就是不同的对象,所以它们的类变量是独立的,不会共享
  1.         for (int i = 0; i < 100; i++) {
  2.             Thread thread = new Thread(new Consumer());
  3.             thread.start();
  4.             threadList.add(thread);
  5.         }

整个程序输出如下:
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
End........
terminal=1000000

点击(此处)折叠或打开

  1. public class Demo {
  2.     
  3.     private static Integer terminal = 0;
  4.     private static byte[] lock = new byte[0];

  5.     public class Producer implements Runnable {

  6.      @Override
  7.      public void run() {
  8.      }
  9.     }
  10.     
  11.     public class Consumer implements Runnable {
  12.         int c = 0;

  13.         @Override
  14.      public void run() {
  15.             for (int i = 0; i < 100000; i++) {
  16.                 synchronized (lock) {
  17.                     terminal++;
  18.                 }
  19.                 c++;
  20.             }
  21.             
  22.             System.out.println("c=" + c);
  23.         }
  24.     }
  25.     
  26.     public void doExcecute() {
  27.         
  28.         List<Thread> threadList = new ArrayList<Thread>();
  29.         for (int i = 0; i < 10; i++) {
  30.             Thread thread = new Thread(new Producer());
  31.             thread.start();
  32.             threadList.add(thread);
  33.         }
  34.         
  35. //        Consumer consumer = new Consumer();
  36.         for (int i = 0; i < 100; i++) {
  37.             Thread thread = new Thread(new Consumer());
  38.             thread.start();
  39.             threadList.add(thread);
  40.         }
  41.         
  42.         //join, is it works?
  43.         for (Thread t : threadList) {
  44.             try {
  45.                 t.join();
  46.             } catch (InterruptedException e) {
  47.                 e.printStackTrace();
  48.             }
  49.         }
  50.         
  51.         System.out.println("End........");
  52.         System.out.println("terminal=" + terminal);
  53.     }
  54.     
  55.     public static void main(String[] args) {
  56.         Demo demo = new Demo();
  57.         demo.doExcecute();
  58.     }
  59. }

加入代码如下:
注意每个Consumer线程是共用的一个对象
  1.         Consumer consumer = new Consumer();
  2.         for (int i = 0; i < 10; i++) {
  3.             Thread thread = new Thread(consumer);
  4.             thread.start();
  5.             threadList.add(thread);
  6.         }
这时的输出是:
c=375383
c=444744
c=626293
c=815561
c=847026
c=941326
c=956545
c=978094
c=982533
c=998304
End........
terminal=1000000

从这个比较看出,第二种方法,类变量是多个线程共享的,不是独立的
至于选择哪种方案,当然是根据实际情况来选择了

点击(此处)折叠或打开

  1. public class Demo {
  2.     
  3.     private static Integer terminal = 0;
  4.     private static byte[] lock = new byte[0];

  5.     public class Producer implements Runnable {

  6.      @Override
  7.      public void run() {
  8.      }
  9.     }
  10.     
  11.     public class Consumer implements Runnable {
  12.         int c = 0;

  13.         @Override
  14.      public void run() {
  15.             for (int i = 0; i < 100000; i++) {
  16.                 synchronized (lock) {
  17.                     terminal++;
  18.                 }
  19.                 c++;
  20.             }
  21.             
  22.             System.out.println("c=" + c);
  23.         }
  24.     }
  25.     
  26.     public void doExcecute() {
  27.         
  28.         List<Thread> threadList = new ArrayList<Thread>();
  29.         for (int i = 0; i < 10; i++) {
  30.             Thread thread = new Thread(new Producer());
  31.             thread.start();
  32.             threadList.add(thread);
  33.         }
  34.         
  35.         Consumer consumer = new Consumer();
  36.         for (int i = 0; i < 10; i++) {
  37.             Thread thread = new Thread(consumer);
  38.             thread.start();
  39.             threadList.add(thread);
  40.         }
  41.         
  42.         //join, is it works?
  43.         for (Thread t : threadList) {
  44.             try {
  45.                 t.join();
  46.             } catch (InterruptedException e) {
  47.                 e.printStackTrace();
  48.             }
  49.         }
  50.         
  51.         System.out.println("End........");
  52.         System.out.println("terminal=" + terminal);
  53.     }
  54.     
  55.     public static void main(String[] args) {
  56.         Demo demo = new Demo();
  57.         demo.doExcecute();
  58.     }
  59. }


阅读(1459) | 评论(0) | 转发(0) |
0

上一篇:关于STL的一些零碎

下一篇:GCC编译命令

给主人留下些什么吧!~~