实现T1线程执行后再执行T2线程,之后再执行T3线程
2017-05-14 20:40
375 查看
public class TestJoin
{
public static void main(String[] args)
{
Store sotre = new Store();
T1 t1= new T1(sotre, "线程1");
Thread t2 = new Thread(sotre, "线程2");
Thread t3 = new Thread(sotre, "线程3");
try
{
//t1先启动
t1.start();
t1.join();
//t2
t2.start();
t2.join();
//t3
t3.start();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
/**
*
*
* <p>
* Title:共享资源
* </p>
* <p>
* Description:it.cn.itcast.Store.java
* </p>
* <p>
* Copyright: 2017-5-14下午6:09:27
* </p>
* <p>
* Company:
* </p>
*
* @author King
*/
public class Store{
private int count;
private int size;
public Store() {
this.count = 0;
this.size = 10;
}
public Store(int count, int size) {
super();
this.count = count;
this.size = size;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void addStore() {
count++;
System.out.println("thread- add-->"+Thread.currentThread().getName());
}
public void removeStore() {
count--;
System.out.println("thread---remove-->"+Thread.currentThread().getName());
}
public boolean isFull() {
return this.count >= size;
}
public boolean isEmpty() {
return this.count <= 0;
}
}
class T1 extends Thread{
@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 extends Thread{
@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 extends Thread{
@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();
}
}
}
}
{
public static void main(String[] args)
{
Store sotre = new Store();
T1 t1= new T1(sotre, "线程1");
Thread t2 = new Thread(sotre, "线程2");
Thread t3 = new Thread(sotre, "线程3");
try
{
//t1先启动
t1.start();
t1.join();
//t2
t2.start();
t2.join();
//t3
t3.start();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
/**
*
*
* <p>
* Title:共享资源
* </p>
* <p>
* Description:it.cn.itcast.Store.java
* </p>
* <p>
* Copyright: 2017-5-14下午6:09:27
* </p>
* <p>
* Company:
* </p>
*
* @author King
*/
public class Store{
private int count;
private int size;
public Store() {
this.count = 0;
this.size = 10;
}
public Store(int count, int size) {
super();
this.count = count;
this.size = size;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void addStore() {
count++;
System.out.println("thread- add-->"+Thread.currentThread().getName());
}
public void removeStore() {
count--;
System.out.println("thread---remove-->"+Thread.currentThread().getName());
}
public boolean isFull() {
return this.count >= size;
}
public boolean isEmpty() {
return this.count <= 0;
}
}
class T1 extends Thread{
@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 extends Thread{
@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 extends Thread{
@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();
}
}
}
}
相关文章推荐
- 现有T1、T2、T3三个线程,你怎样保证T2在T1执行完成之后执行,T3在T2执行完后执行?
- 1.如果你有三个线程,分别为T1,T2,T3,如何让线程T2在线程T1之后执行,在线程T3之前执行。
- 1)现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
- 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
- 代码详解の使用CountDownLatch解决面试问题:T1和T2线程执行计算,T3线程计算结果的统计
- 现在有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执行完后执行?
- 1)现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
- 转:java实现三个线程A B C,A线程打印A之后才能B线程打印B,然后接着C线程执行打印C,继而循环下去
- 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载
- 现有三个线程T1,T2,T3 三个线程依次执行
- Java线程暂停一段时间之后继续执行的实现方法
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 有三个线程T1,T2,T3,下面方法可以确保它们按顺序执行的有()该线程继续执行
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- java实现三个线程A B C,A线程打印A之后才能B线程打印B,然后接着C线程执行打印C,继而循环下去
- 使用管道流和多线程完成以下任务要求: 一个线程对象t1 会从控制台中不停的读取信息,读到之后把数据交给管道输出流,把数据写到管道里面 ,另一个线程对象t2 不停的使用管道输入流从管道
- 转:java实现三个线程A B C,A线程打印A之后才能B线程打印B,然后接着C线程执行打印C,继而循环下去