java 线程顺序_Java 指定线程执行顺序(三种方式)其实是四种

packagecom.zyh.controller.test;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;/*** student threads

*

*@author1101399

* @CreateDate 2018-7-30 下午2:09:01*/

public classThreadTestOne {public static void main(String[] args) throwsInterruptedException {final ThreadTest A1 = new ThreadTest("A1");final ThreadTest A2 = new ThreadTest("A2");final ThreadTest A3 = new ThreadTest("A3");

// A1.start(); TODO 屏蔽

// A2.start(); TODO 屏蔽

A1.join();

A2.join();

System.out.println("方法一实现多线程");if (!A1.isAlive()) {//A1 线程不存在的时候控制台打印一条信息

System.out.println("A1执行完毕?!");

}final Thread B1 = new Thread(new RunnableTest("B1"));

// B1.start(); TODO 屏蔽final Thread B2 = new Thread(new RunnableTest("B2"));

// B2.start(); TODO 屏蔽

B1.join();

B2.join();

System.out.println("方法二实现多线程");/*** 直接实现线程的开辟 FIXME*/

final Thread C1 = new Thread(newRunnable() {

@Overridepublic voidrun() {try{

A1.join();

A2.join();

B1.join();

B2.join();

}catch(InterruptedException e) {

e.printStackTrace();

}for (int i = 0; i < 15; i++) {

System.out.println("··············");

}

}

});

// C1.start(); TODO 屏蔽

C1.join();

System.out.println("方法三实现多线程");

System.out.println("线程池的应用");//线程池的学习&应用 TODO 依次执行

ExecutorService executor =Executors.newSingleThreadExecutor();

executor.submit(A1);

executor.submit(A2);

executor.submit(A3);

executor.execute(B1);//这种样子的线程类就是不执行

executor.execute(A1);

executor.submit(B1);// 这三种线程的实现方式之前不能 XXX start()启动线程

executor.submit(B2);// 这三种线程的实现方式之前不能 XXX start()启动线程

executor.submit(C1);// 这三种线程的实现方式之前不能 XXX start()启动线程

executor.shutdown();//停止传入任务//executor.shutdownNow();//停止线程池-对线程池说STOP//会导致线程池中第一个线程的sleep出现sleep interrupted异常//该函数的核心是:它向该线程发起interrupt()请求,而sleep()方法遇到有interrupt()请求时,会抛出InterruptedException(),并继续往下执行//运行到这条语句直接停止线程池-检测线程停止后执行的join()函数毫无意义,不能生效

}/*** 继承Thread来实现多线程编程 FIXME*/

public static class ThreadTest extendsThread {publicString nameOne;publicStringBuffer nameTwo;publicStringBuilder nameThree;private longtime;//构造函数

publicThreadTest(String name) {this.nameOne =name;

}//构造函数

public ThreadTest(String name, longtime) {this.nameOne =name;this.time =time;

}

@Overridepublic voidrun() {for (int i = 0; i < 5; i++) {

System.out.println(this.nameOne + " Thread运行第 " + i + " 次!");try{if (this.time != 0) {

sleep(this.time +i);

System.out.println(this.nameOne + "-time-" + (time +i));

}else{//sleep((int) Math.random() * 1000);

sleep(50);

System.out

.println(this.nameOne + "-random-" + (int) (Math.random() * 1000));

}

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}/*** 实现接口Runnable来实现多线程编程 FIXME*/

public static class RunnableTest implementsRunnable {publicString nameOne;publicStringBuffer nameTwo;publicStringBuilder nameThree;private longtime;publicRunnableTest(String name) {this.nameOne =name;

}public RunnableTest(String name, longtime) {this.nameOne =name;this.time =time;

}

@Overridepublic voidrun() {for (int i = 0; i < 5; i++) {

System.out.println(this.nameOne + " Runnable运行第 " + i + " 次!");try{if (this.time != 0) {

Thread.sleep(this.time +i);

System.out.println(this.nameOne + "-time-" + (time +i));

}else{

Thread.sleep((int) Math.random() * 1000);

System.out

.println(this.nameOne + "-random-" + (int) (Math.random() * 1000));

}

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值