java多线程问题:下面代码我运行的结果和书上给的不一样
classThreadTestimplementsRunnable{publicvoidrun(){//Stringstr=newString();inti=0;for(...
class ThreadTest implements Runnable
{
public void run()
{
//String str = new String();
int i = 0;
for(int x =0;x < 10;x++)
{
System.out.println(Thread.currentThread().getName()+"--->>"+ i++);
}
}
}
public class ThreadJoin extends Thread
{
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest t = new ThreadTest();
Thread pp = new Thread(t);
pp.start();
int i = 0;
for(int x = 0;x < 10;x++)
{
if(i == 5)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread"+i);
i++;
}
}
}
我运行结果顺序:
main Thread0
Thread-0--->>0
main Thread1
Thread-0--->>1
main Thread2
Thread-0--->>2
main Thread3
Thread-0--->>3
main Thread4
Thread-0--->>4
Thread-0--->>5
Thread-0--->>6
Thread-0--->>7
Thread-0--->>8
Thread-0--->>9
main Thread5
main Thread6
main Thread7
main Thread8
main Thread9
书上顺序:
main Thread0
main Thread1
main Thread2
main Thread3
main Thread4
Thread-0--->>0
Thread-0--->>1
Thread-0--->>2
Thread-0--->>3
Thread-0--->>4
Thread-0--->>5
Thread-0--->>6
Thread-0--->>7
Thread-0--->>8
Thread-0--->>9
main Thread5
main Thread6
main Thread7
main Thread8
main Thread9 展开
{
public void run()
{
//String str = new String();
int i = 0;
for(int x =0;x < 10;x++)
{
System.out.println(Thread.currentThread().getName()+"--->>"+ i++);
}
}
}
public class ThreadJoin extends Thread
{
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest t = new ThreadTest();
Thread pp = new Thread(t);
pp.start();
int i = 0;
for(int x = 0;x < 10;x++)
{
if(i == 5)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread"+i);
i++;
}
}
}
我运行结果顺序:
main Thread0
Thread-0--->>0
main Thread1
Thread-0--->>1
main Thread2
Thread-0--->>2
main Thread3
Thread-0--->>3
main Thread4
Thread-0--->>4
Thread-0--->>5
Thread-0--->>6
Thread-0--->>7
Thread-0--->>8
Thread-0--->>9
main Thread5
main Thread6
main Thread7
main Thread8
main Thread9
书上顺序:
main Thread0
main Thread1
main Thread2
main Thread3
main Thread4
Thread-0--->>0
Thread-0--->>1
Thread-0--->>2
Thread-0--->>3
Thread-0--->>4
Thread-0--->>5
Thread-0--->>6
Thread-0--->>7
Thread-0--->>8
Thread-0--->>9
main Thread5
main Thread6
main Thread7
main Thread8
main Thread9 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询