多线程顺序打印ABC的三种实现---join方法
2016-08-05 10:03
696 查看
使用join方法,让三个线程之间存在等待关系
代码如下:
[java] view
plain copy
package com.zcj.join;
public class JoinTest {
public static void main(String[] args) {
ThreadA threadA = new ThreadA();
ThreadB threadB = new ThreadB(threadA);
ThreadC threadC = new ThreadC(threadB);
threadA.start();
threadB.start();
threadC.start();
}
}
class ThreadA extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("A");
}
}
class ThreadB extends Thread{
private ThreadA threadA;
public ThreadB(ThreadA threadA){
this.threadA =threadA;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
threadA.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("B");
}
}
class ThreadC extends Thread{
private ThreadB threadB;
public ThreadC(ThreadB threadB){
this.threadB =threadB;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
threadB.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("C");
}
}
运行结果:
[plain] view
plain copy
A
B
C
代码如下:
[java] view
plain copy
package com.zcj.join;
public class JoinTest {
public static void main(String[] args) {
ThreadA threadA = new ThreadA();
ThreadB threadB = new ThreadB(threadA);
ThreadC threadC = new ThreadC(threadB);
threadA.start();
threadB.start();
threadC.start();
}
}
class ThreadA extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("A");
}
}
class ThreadB extends Thread{
private ThreadA threadA;
public ThreadB(ThreadA threadA){
this.threadA =threadA;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
threadA.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("B");
}
}
class ThreadC extends Thread{
private ThreadB threadB;
public ThreadC(ThreadB threadB){
this.threadB =threadB;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
threadB.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("C");
}
}
运行结果:
[plain] view
plain copy
A
B
C
相关文章推荐
- java多线程---顺序打印ABC的三种实现---join方法
- java多线程---顺序打印ABC的三种实现---volatile方式
- java多线程---顺序打印ABC的三种实现---ReentrantLock方式
- 多线程交替打印ABC的多种实现方法
- java多线程---顺序打印ABC的三种实现---synchronized方式
- 多线程交替打印ABC的多种实现方法
- 多线程题 3个线程各打印一种字母,按顺序打印若干次ABC 的2种实现
- java 多线程顺序打印ABC
- html或jsp实现打印三种方法
- php三种实现多线程类似的方法
- JAVA 多线程开篇 -从按顺序打印ABC开始
- 三种iOS多线程实现的方法
- 三种iOS多线程实现方法
- java实现多线程三种方法
- html或jsp实现打印三种方法
- 基于条件变量和互斥量同步的三线程顺序打印abc(10次)实现
- web 打印,web打印控件的三种实现方法
- 多线程顺序打印ABC
- ios开发系列之多线程的三种实现方法
- 练习:利用多线程 按照顺序将 ABC 打印十遍 ,体会 lock 和 condition 的用法