多线程-join跟yield
多线程--join和yield
线程调用yield后暂停执行权
//A线程遇到B线程join A线程让出执行权 直到B线程执行完毕再执行 public class JoinDemo { public static void main(String[] args) throws Exception { Demo d=new Demo(); Thread t1=new Thread(d); Thread t2=new Thread(d); t1.setName("t1"); t2.setName("t2"); t1.start(); //t1.join(); t2.start(); t1.join(); for(int i=0;i<80;i++) { System.out.println("main----------"+" i="+i); } } } class Demo implements Runnable { public void run() { for(int i=0;i<50;i++) { System.out.println(Thread.currentThread().getName()+" i="+i); } } }
线程调用yield后暂停执行权
class YieldDemo { public static void main(String[] args) { Demo d=new Demo(); Thread t1=new Thread(d); Thread t2=new Thread(d); t1.start(); t2.start(); } } class Demo implements Runnable { public void run() { for(int i=0;i<100;i++) { Thread.yield(); System.out.println(Thread.currentThread().getName()+"..."+i); } } }