Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2468926
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类: C/C++

2013-06-25 16:53:22

这个多线程的目的:多线程执行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. }


阅读(3352) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~