因为刚学习java不久,对于多线程有一点不解,就是如果各个线程的优先级如果一样的话(包括主线程),那么如果主线程和其他线程同时为就绪状态,当然此时有一个线程在运行,当这个线程处于阻塞状态时,究竟是主线程还是其他线程成为运行状态?下面有一段代码,为何主线程不能在当前线程处于阻塞状态时开始运行,而一定要等待两个线程运行结束,主线程睡眠时间无论多少都会等待其他线程运行结束吗,为什么会是这样?如果不想让主线程等待的话该怎样做呢,请各位指点
package occupycpu;
public class Machine extends Thread{
private static StringBuffer log=new StringBuffer();
private static int count=0; public void run(){
for(int a=0;a<20;a++){
log.append(currentThread().getName()+":"+a+" ");
if(++count %10==0)log.append("\n");
try{
sleep(100);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
public static void main(String args[])throws Exception{
Machine machine1=new Machine();
Machine machine2=new Machine();
machine1.setName("m1");
machine2.setName("m2");
machine1.start();
machine2.start();
while(machine1.isAlive() || machine2.isAlive())
Thread.sleep(5); //主线程睡眠5秒,等待machine1和machine2线程运行结束
System.out.println(log);
}
}
package occupycpu;
public class Machine extends Thread{
private static StringBuffer log=new StringBuffer();
private static int count=0; public void run(){
for(int a=0;a<20;a++){
log.append(currentThread().getName()+":"+a+" ");
if(++count %10==0)log.append("\n");
try{
sleep(100);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
public static void main(String args[])throws Exception{
Machine machine1=new Machine();
Machine machine2=new Machine();
machine1.setName("m1");
machine2.setName("m2");
machine1.start();
machine2.start();
while(machine1.isAlive() || machine2.isAlive())
Thread.sleep(5); //主线程睡眠5秒,等待machine1和machine2线程运行结束
System.out.println(log);
}
}
但是你不能肯定JVM是调度哪个线程执行代码,这个是不确定的。在多线程环境中,如果你不使用同步代码(wait、sychronized之类),你不应该猜测代码的执行顺序所以m1, m2的顺序会乱
我想用调用yeild()方法应该行的。——————————————————————继续探讨,我也是初学者
修改后的代码如下:
package occupycpu;
public class Machine extends Thread{
private static StringBuffer log=new StringBuffer();
private static int count=0; public void run(){
for(int a=0;a <20;a++){
log.append(currentThread().getName()+":"+a+" ");
if(++count %10==0)log.append("\n");
try{
sleep(200);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
public static void main(String args[])throws Exception{
Machine machine1=new Machine();
Machine machine2=new Machine();
machine1.setName("m1");
machine2.setName("m2");
machine1.start();
machine2.start();
machine1.join();
System.out.println(log);
}
}
private static StringBuffer log=new StringBuffer();
private static int count=0; public void run(){
for(int a=0;a <20;a++){
log.append(currentThread().getName()+":"+a+" ");
if(++count %10==0)log.append("\n");
try{
sleep(100);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
public static void waitForit(Machine machine11,Machine machine22)
{try
{machine11.join();
}catch(InterruptedException e){}
machine22.start();
}
public static void main(String args[])throws Exception{
Machine machine1=new Machine();
Machine machine2=new Machine();
machine1.setName("m1");
machine2.setName("m2");
machine1.start();
machine2.waitForit(machine1,machine2);
// 1、 while(machine1.isAlive() || machine2.isAlive()) Thread.sleep(5); //主线程睡眠5秒,等待machine1和machine2线程运行结束
System.out.println(log);
}
}
哈哈 我也实现了 不过他的资源被互斥了
----
错误!!线程中的代码执行是需要时间的,也就是append和println需要花费时间的,而且sleep并非一个精确的函数,它只是保证线程最小sleep这么多时间,在到达这个时间时并不意味着线程的代码可以马上得到执行。所以我已经强调了,你不能对多线程中的执行时间作任何的假设!