多线程编程join
join保证线程顺序执行
@Slf4j
public class JoinA implements Runnable{
@Override
public void run() {
log.info("洗苹果");
}
}
@Slf4j
public class JoinB implements Runnable {
@Override
public void run() {
log.info("吃苹果");
}
}
public class JoinTest {
public static void main(String[] args){
try{
Thread threadA =new Thread( new JoinA());
Thread threadB =new Thread( new JoinB());
threadA.start();
threadA.join();
threadB.start();
}catch (Exception e){
}
}
}
join可以保证线程顺序执行,保证线程A执行完后,在执行线程B,
join(long)方法
@Slf4j
public class JoinA implements Runnable{
@Override
public void run() {
try {
Thread.sleep(6000);
}catch (Exception e){
}
log.info("洗苹果");
}
}
public class JoinTest {
public static void main(String[] args){
try{
Thread threadA =new Thread( new JoinA());
Thread threadB =new Thread( new JoinB());
threadA.start();
threadA.join(1);
threadB.start();
}catch (Exception e){
}
}
}
join(long) 方法会释放锁,会在线程A执行1秒后,释放锁,B线程执行,