一个java线程的小例子 本帖最后由 Amo860129 于 2011-10-26 02:29:26 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 pp.start();不代表线程马上被执行~~而是等待资源执行~~~if(i==100) 也是到 100 才执行pp.join() join 方法的作用是使得本来异步执行的线程变成同步执行~~~输出结果没啥问题~~~ public class JoinThread{ public static void main(String[] args) { ThreadTest t=new ThreadTest(); Thread pp=new Thread(t); pp.start(); int i=0; while(true) { // 这里你只运行i == 100时才加入其他线程 // 导致前100个都是main 线程 if(i==100) { try { pp.join(); } catch(Exception e) { System.out.println(e.getMessage()); } } System.out.println("main Thread " + i++); } }}class ThreadTest implements Runnable{ public void run() { String str=new String(); int i=0; while(true) { System.out.println(Thread.currentThread().getName()+ " " + i++); } }} public class JoinThread{ public static void main(String[] args) { ThreadTest t=new ThreadTest(); Thread pp=new Thread(t); pp.start(); int i=0; while(true) { // 这里你只运行i == 100时才加入其他线程 // 导致前100个都是main 线程 if(i==100) { try { pp.join(); } catch(Exception e) { System.out.println(e.getMessage()); } } System.out.println("main Thread " + i++); } }}class ThreadTest implements Runnable{ public void run() { String str=new String(); int i=0; while(true) { System.out.println(Thread.currentThread().getName()+ " " + i++); } }} public class JoinThread{ public static void main(String[] args) { ThreadTest t=new ThreadTest(); Thread pp=new Thread(t); pp.start(); int i=0; while(true) { // 这里你只运行i == 100时才加入其他线程 // 导致前100个都是main 线程 if(i==100) { try { pp.join(); } catch(Exception e) { System.out.println(e.getMessage()); } } System.out.println("main Thread " + i++); } }}class ThreadTest implements Runnable{ public void run() { String str=new String(); int i=0; while(true) { System.out.println(Thread.currentThread().getName()+ " " + i++); } }} 不同意的你的观点在没有使用join方法之前 两个线程的确是交替执行的~~为啥楼主说先出来0到99 那是因为 pp.start 是等待执行不是马上执行 我上面已经说过了。现在因为计算机的能力太强大了 以至于 等待执行的时间片中~~WHILE 已经把0-99全部循环完了想看到交替执行的效果 你把 while(true) {// if(i==100)// {// try// {// pp.join();// }// catch(Exception e)// {// System.out.println(e.getMessage());// }// } System.out.println("main Thread " + i++); } 不同意的你的观点在没有使用join方法之前 两个线程的确是交替执行的~~为啥楼主说先出来0到99 那是因为 pp.start 是等待执行不是马上执行 我上面已经说过了。现在因为计算机的能力太强大了 以至于 等待执行的时间片中~~WHILE 已经把0-99全部循环完了想看到交替执行的效果 你把 while(true) {// if(i==100)// {// try// {// pp.join();// }// catch(Exception e)// {// System.out.println(e.getMessage());// }// } System.out.println("main Thread " + i++); } 而且你有两个while(true)的死循环, 执行到pp.start();一直在执行ThreadTest的while循环呀 public class JoinThread{ public static void main(String[] args) { ThreadTest t=new ThreadTest(); Thread pp=new Thread(t); pp.start(); int i=0; while(i<20) { if(i==10) { try { pp.join(); } catch(Exception e) { System.out.println(e.getMessage()); } } System.out.println("main Thread " + i++); } }}class ThreadTest implements Runnable{ public void run() { String str=new String(); int i=0; while(i<10) { System.out.println(Thread.currentThread().getName()+ " " + i++); } }} 100太小了,其实题目的意思是想让你看到2个线程开始是交替执行的,当main中的i到100的时候main等待另一个线程执行,知道等待的线程执行完了才继续main的执行。由于100太小了,还没轮到第二个线程执行,main就开始等待了,所以输出楼主这样的结果。或者你可以在main中i==50的时候Thread.sleep(10);一下,就能看到不同的输出了。 ok,果然是电脑太牛逼了,在短短的时间内就执行完毕,100改成大点的数值就KO了,感谢大家耐心讲解~ 如何判断一个String是json格式还是XML格式 随机数问题 为什么会有如下错误呢? for循环问题 !!!急~请教JDBC时时查询ORACLE数据库某表的问题! 初学者的送分题,先来者先得 关于包的问题??? 在java中au声音暂停怎么做? 简单问题,请高手进来看看: 如何用java编写一个飞机游戏? 这个正则我要疯了 我的物理地址两天变了俩次,求解!!!
if(i==100) 也是到 100 才执行pp.join()
join 方法的作用是使得本来异步执行的线程变成同步执行~~~
输出结果没啥问题~~~
public class JoinThread
{
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(true)
{
// 这里你只运行i == 100时才加入其他线程
// 导致前100个都是main 线程
if(i==100)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread " + i++);
}
}
}
class ThreadTest implements Runnable
{
public void run()
{
String str=new String();
int i=0;
while(true)
{
System.out.println(Thread.currentThread().getName()+ " " + i++);
}
}
}
public class JoinThread
{
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(true)
{
// 这里你只运行i == 100时才加入其他线程
// 导致前100个都是main 线程
if(i==100)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread " + i++);
}
}
}
class ThreadTest implements Runnable
{
public void run()
{
String str=new String();
int i=0;
while(true)
{
System.out.println(Thread.currentThread().getName()+ " " + i++);
}
}
}
public class JoinThread
{
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(true)
{
// 这里你只运行i == 100时才加入其他线程
// 导致前100个都是main 线程
if(i==100)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread " + i++);
}
}
}
class ThreadTest implements Runnable
{
public void run()
{
String str=new String();
int i=0;
while(true)
{
System.out.println(Thread.currentThread().getName()+ " " + i++);
}
}
}
在没有使用join方法之前 两个线程的确是交替执行的~~
为啥楼主说先出来0到99 那是因为 pp.start 是等待执行不是马上执行 我上面已经说过了。
现在因为计算机的能力太强大了 以至于 等待执行的时间片中~~WHILE 已经把0-99全部循环完了
想看到交替执行的效果 你把 while(true)
{
// if(i==100)
// {
// try
// {
// pp.join();
// }
// catch(Exception e)
// {
// System.out.println(e.getMessage());
// }
// }
System.out.println("main Thread " + i++);
}
在没有使用join方法之前 两个线程的确是交替执行的~~
为啥楼主说先出来0到99 那是因为 pp.start 是等待执行不是马上执行 我上面已经说过了。
现在因为计算机的能力太强大了 以至于 等待执行的时间片中~~WHILE 已经把0-99全部循环完了
想看到交替执行的效果 你把 while(true)
{
// if(i==100)
// {
// try
// {
// pp.join();
// }
// catch(Exception e)
// {
// System.out.println(e.getMessage());
// }
// }
System.out.println("main Thread " + i++);
}
{
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(i<20)
{
if(i==10)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread " + i++);
}
}
}
class ThreadTest implements Runnable
{
public void run()
{
String str=new String();
int i=0;
while(i<10)
{
System.out.println(Thread.currentThread().getName()+ " " + i++);
}
}
}
由于100太小了,还没轮到第二个线程执行,main就开始等待了,所以输出楼主这样的结果。
或者你可以在main中i==50的时候Thread.sleep(10);一下,就能看到不同的输出了。