现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
2016-09-27 19:43
471 查看
使用join()方法
public class TestJoin
{
public static void main(String[] args)
{
Thread t1 = new Thread(new T1(), "线程1");
Thread t2 = new Thread(new T2(), "线程2");
Thread t3 = new Thread(new T3(), "线程3");
try
{
//t1先启动
t1.start();
t1.join();
//t2
t2.start();
t2.join();
//t3
t3.start();
t3.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class T1 implements Runnable{
@Override
public void run()
{
for (int i = 0; i < 5; i++)
{
System.out.println(Thread.currentThread().getName()+": "+i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class T2 implements Runnable{
@Override
public void run()
{
for (int j = 5; j < 10; j++)
{
System.out.println(Thread.currentThread().getName()+": "+j);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class T3 implements Runnable{
@Override
public void run()
{
for (int i = 10; i < 15; i++)
{
System.out.println(Thread.currentThread().getName()+": "+i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}还有一种方式,在t3开始前join t2,在t2开始前join t1
public class TestJoin2
{
public static void main(String[] args)
{
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1");
}
});
final Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
//引用t1线程,等待t1线程执行完
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t2");
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
//引用t2线程,等待t2线程执行完
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t3");
}
});
t3.start();
t2.start();
t1.start();
}
}
转自:http://www.cnblogs.com/yzlpersonal/p/5231681.html
4000
public class TestJoin
{
public static void main(String[] args)
{
Thread t1 = new Thread(new T1(), "线程1");
Thread t2 = new Thread(new T2(), "线程2");
Thread t3 = new Thread(new T3(), "线程3");
try
{
//t1先启动
t1.start();
t1.join();
//t2
t2.start();
t2.join();
//t3
t3.start();
t3.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class T1 implements Runnable{
@Override
public void run()
{
for (int i = 0; i < 5; i++)
{
System.out.println(Thread.currentThread().getName()+": "+i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class T2 implements Runnable{
@Override
public void run()
{
for (int j = 5; j < 10; j++)
{
System.out.println(Thread.currentThread().getName()+": "+j);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class T3 implements Runnable{
@Override
public void run()
{
for (int i = 10; i < 15; i++)
{
System.out.println(Thread.currentThread().getName()+": "+i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}还有一种方式,在t3开始前join t2,在t2开始前join t1
public class TestJoin2
{
public static void main(String[] args)
{
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1");
}
});
final Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
//引用t1线程,等待t1线程执行完
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t2");
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
//引用t2线程,等待t2线程执行完
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t3");
}
});
t3.start();
t2.start();
t1.start();
}
}
转自:http://www.cnblogs.com/yzlpersonal/p/5231681.html
4000
相关文章推荐
- 现在有T1、T2、T3三个线程,怎样保证T2在T1执行完后执行,T3在T2执行完后执行
- 1)现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
- 1)现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
- 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
- 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行
- 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行
- 现有T1、T2、T3三个线程,你怎样保证T2在T1执行完成之后执行,T3在T2执行完后执行?
- 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 1.如果你有三个线程,分别为T1,T2,T3,如何让线程T2在线程T1之后执行,在线程T3之前执行。
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 现有三个线程T1,T2,T3 三个线程依次执行
- 代码详解の使用CountDownLatch解决面试问题:T1和T2线程执行计算,T3线程计算结果的统计
- 实现T1线程执行后再执行T2线程,之后再执行T3线程
- 有三个线程T1,T2,T3,下面方法可以确保它们按顺序执行的有()该线程继续执行
- 保证三个线程依次按顺序执行
- 如何保证线程按顺序执行
- 怎样区分T1加权像和T2加权像
- C#中添加三个线程同时启动执行某一方法,并依次调用某方法中的循环打印输。