/*
public class Test {
public static void main(String[] args) {
Xc xc = new Xc();
Thread a = new Thread(xc);
Thread b = new Thread(xc);
a.setName("线程1");
b.setName("线程2");
a.start();
b.start();
}
}
class Xc implements Runnable {
public void run() {
for (int i = 1; i <= 30; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
if (i % 5 == 0) {//当满足这一条件时,线程让步。
Thread.yield();
}
}
}
}
*/
public class Test{
public static void main(String[] args){
xc xc=new xc();
Thread a=new Thread(xc);
a.start();
try{
a.join();//阻塞:将当前线程暂停,直至调用join函数所对应的线程执行完毕,才继续执行当前程序。
}
catch(Exception e){}
for(int i=0;i<25;i++){
System.out.println("主线程"+i);
}
}
}
class xc implements Runnable{
public void run(){
for(int i=0;i<20;i++){
System.out.println("子线程:"+i);
}
}
}
JAVA进阶6.5——线程的让步与阻塞
最新推荐文章于 2021-12-03 19:42:31 发布