一个java线程的小例子
正在学java线程,书上说,这个线程由于调用了pp.join()所以说在main线程计数到达100前,是双线程交替执行,main到达100后就只有别的线程执行,但是在我电脑上运行的是,main在一开始以迅雷不及掩耳盗铃之势就100了,没别的线程啥事儿,后来就单单是其他线程在执行。为什么啊,跟我电脑有关吗?
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) { 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++); } }}