犹豫再三,还是决定提问,这两天线程的执行顺序搞的我好晕!!
class Q {
int n;
boolean valueSet=true;
synchronized void put(int n) {
if(!valueSet)
try{
wait();
}catch(InterruptedException e){
System.out.println("Interrupted exception caught.");
}
this.n = n;
System.out.println("Put: " + n);
valueSet=false;
notify();
} synchronized int get() {
if(valueSet)
try{
wait();
}catch(InterruptedException e){
System.out.println("Interrupted exception caught.");
}
System.out.println("Got: " + n);
valueSet=true;
notify();
return n;
}
}
//======================================================
class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
// while(true) {
for(int m=15;m>0;m--)
q.put(i++);
// }
}
}
//======================================================
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
// while(true) {
for(int m=15;m>0;m--)
q.get();
// }
}
}
//======================================================
class PC {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}
//======================================================
在单位电脑的赛洋单核CPU上执行结果:
Press Control-C to stop.
Put: 0
Got: 0
Put: 1
Got: 1
... ...
在家里的AMD双核4000+上的执行结果:
Put: 0
Press Control-C to stop.
Got: 0
Put: 1
Got: 1
... ...
其实这个main的println语句什么时候执行我还是不明白。
println在main方法的最后,据我的理解应该在所有子线程结束之后执行,或在子线程互斥的间隙执行。
main的println为什么会最先执行呢?
子线程的互斥应该没有间隙啊,为什么main的println出线在Put: 0与Got: 0中间呢?
难道主线程和所有子线程的执行顺序都是随机的?
class Q {
int n;
boolean valueSet=true;
synchronized void put(int n) {
if(!valueSet)
try{
wait();
}catch(InterruptedException e){
System.out.println("Interrupted exception caught.");
}
this.n = n;
System.out.println("Put: " + n);
valueSet=false;
notify();
} synchronized int get() {
if(valueSet)
try{
wait();
}catch(InterruptedException e){
System.out.println("Interrupted exception caught.");
}
System.out.println("Got: " + n);
valueSet=true;
notify();
return n;
}
}
//======================================================
class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
// while(true) {
for(int m=15;m>0;m--)
q.put(i++);
// }
}
}
//======================================================
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
// while(true) {
for(int m=15;m>0;m--)
q.get();
// }
}
}
//======================================================
class PC {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}
//======================================================
在单位电脑的赛洋单核CPU上执行结果:
Press Control-C to stop.
Put: 0
Got: 0
Put: 1
Got: 1
... ...
在家里的AMD双核4000+上的执行结果:
Put: 0
Press Control-C to stop.
Got: 0
Put: 1
Got: 1
... ...
其实这个main的println语句什么时候执行我还是不明白。
println在main方法的最后,据我的理解应该在所有子线程结束之后执行,或在子线程互斥的间隙执行。
main的println为什么会最先执行呢?
子线程的互斥应该没有间隙啊,为什么main的println出线在Put: 0与Got: 0中间呢?
难道主线程和所有子线程的执行顺序都是随机的?
解决方案 »
- 简单数据岛显示
- socket中client的问题
- java中GUI需要花大量时间深究吗?
- 实在想不明白了才来发问:Visual Age中发行和版本化有什么区别?
- 《thinking in java 2nd》里的一道题,请大家帮忙看一下
- 介绍基本 java 初学者看的书 !
- 请问哪里有java docs 的下载,中文版的,它是java的类库手册么?
- 我想调用com.sun.java.swing.*包中的类,如JFrame,JButton,是不是要先安装JFC?哪里有,怎么装?
- SOS:Graphics()该如何使用呀
- 如何在winNT中运行jdk?
- 用dom4j读取解析URL地址的xml时,连接超时!
- 如何实现JPanel按比例放大缩小,其内部的组件(如JButton等)的位置和大小也按比例放大缩小。
而start的线程需要做一系列的动作,所以main中靠近start的代码通常比Thread中的代码先执行。