这个多线程的目的:多线程执行Consumer,每个线程除了terminal变量之外,其他变量都是独立的
方案:
1. 把terminal设置成static变量,这样所有Consumer线程共享它
2. lock变量用来做
synchronized同步
注意,每个线程是new出来的,也就是不同的对象,所以它们的类变量是独立的,不会共享
-
for (int i = 0; i < 100; i++) {
-
Thread thread = new Thread(new Consumer());
-
thread.start();
-
threadList.add(thread);
-
}
整个程序输出如下:
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
c=100000
End........
terminal=1000000
-
public class Demo {
-
-
private static Integer terminal = 0;
-
private static byte[] lock = new byte[0];
-
-
public class Producer implements Runnable {
-
-
@Override
-
public void run() {
-
}
-
}
-
-
public class Consumer implements Runnable {
-
int c = 0;
-
-
@Override
-
public void run() {
-
for (int i = 0; i < 100000; i++) {
-
synchronized (lock) {
-
terminal++;
-
}
-
c++;
-
}
-
-
System.out.println("c=" + c);
-
}
-
}
-
-
public void doExcecute() {
-
-
List<Thread> threadList = new ArrayList<Thread>();
-
for (int i = 0; i < 10; i++) {
-
Thread thread = new Thread(new Producer());
-
thread.start();
-
threadList.add(thread);
-
}
-
-
// Consumer consumer = new Consumer();
-
for (int i = 0; i < 100; i++) {
-
Thread thread = new Thread(new Consumer());
-
thread.start();
-
threadList.add(thread);
-
}
-
-
//join, is it works?
-
for (Thread t : threadList) {
-
try {
-
t.join();
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
-
-
System.out.println("End........");
-
System.out.println("terminal=" + terminal);
-
}
-
-
public static void main(String[] args) {
-
Demo demo = new Demo();
-
demo.doExcecute();
-
}
-
}
加入代码如下:
注意每个Consumer线程是共用的一个对象
-
Consumer consumer = new Consumer();
-
for (int i = 0; i < 10; i++) {
-
Thread thread = new Thread(consumer);
-
thread.start();
-
threadList.add(thread);
-
}
这时的输出是:
c=375383
c=444744
c=626293
c=815561
c=847026
c=941326
c=956545
c=978094
c=982533
c=998304
End........
terminal=1000000
从这个比较看出,第二种方法,类变量是多个线程共享的,不是独立的
至于选择哪种方案,当然是根据实际情况来选择了
-
public class Demo {
-
-
private static Integer terminal = 0;
-
private static byte[] lock = new byte[0];
-
-
public class Producer implements Runnable {
-
-
@Override
-
public void run() {
-
}
-
}
-
-
public class Consumer implements Runnable {
-
int c = 0;
-
-
@Override
-
public void run() {
-
for (int i = 0; i < 100000; i++) {
-
synchronized (lock) {
-
terminal++;
-
}
-
c++;
-
}
-
-
System.out.println("c=" + c);
-
}
-
}
-
-
public void doExcecute() {
-
-
List<Thread> threadList = new ArrayList<Thread>();
-
for (int i = 0; i < 10; i++) {
-
Thread thread = new Thread(new Producer());
-
thread.start();
-
threadList.add(thread);
-
}
-
-
Consumer consumer = new Consumer();
-
for (int i = 0; i < 10; i++) {
-
Thread thread = new Thread(consumer);
-
thread.start();
-
threadList.add(thread);
-
}
-
-
//join, is it works?
-
for (Thread t : threadList) {
-
try {
-
t.join();
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
-
-
System.out.println("End........");
-
System.out.println("terminal=" + terminal);
-
}
-
-
public static void main(String[] args) {
-
Demo demo = new Demo();
-
demo.doExcecute();
-
}
-
}
阅读(1453) | 评论(0) | 转发(0) |