Java实现指定线程执行顺序的三种方式示例
作者:Leon-Zheng 发布时间:2021-08-16 15:11:34
标签:Java,线程,顺序
本文实例讲述了Java实现指定线程执行顺序的三种方式。分享给大家供大家参考,具体如下:
方法一:通过共享对象锁加上可见变量来实现。
public class MyService {
private volatile int orderNum = 1;
public synchronized void methodA() {
try {
while (orderNum != 1) {
wait();
}
for (int i = 0; i < 2; i++) {
System.out.println("AAAAA");
}
orderNum = 2;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void methodB() {
try {
while (orderNum != 2) {
wait();
}
for (int i = 0; i < 2; i++) {
System.out.println("BBBBB");
}
orderNum = 3;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void methodC() {
try {
while (orderNum != 3) {
wait();
}
for (int i = 0; i < 2; i++) {
System.out.println("CCCCC");
}
orderNum = 1;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import service.MyService;
public class ThreadAA extends Thread {
private MyService dbtools;
public ThreadAA(MyService dbtools) {
super();
this.dbtools = dbtools;
}
@Override
public void run() {
dbtools.methodA();
}
}
import service.MyService;
public class ThreadBB extends Thread {
private MyService dbtools;
public ThreadBB(MyService dbtools) {
super();
this.dbtools = dbtools;
}
@Override
public void run() {
dbtools.methodB();
}
}
import service.MyService;
public class ThreadCC extends Thread {
private MyService dbtools;
public ThreadCC(MyService dbtools) {
this.dbtools = dbtools;
}
@Override
public void run() {
dbtools.methodC();
}
}
import extthread.ThreadCC;
import service.MyService;
import extthread.ThreadAA;
import extthread.ThreadBB;
public class Run {
public static void main(String[] args) {
MyService myService = new MyService();
for (int i = 0; i < 2; i++) {
ThreadBB output = new ThreadBB(myService);
output.start();
ThreadAA input = new ThreadAA(myService);
input.start();
ThreadCC threadCC = new ThreadCC(myService);
threadCC.start();
}
}
}
执行结果:
可以看到线程的启动按顺序执行了。共享对象锁,可以保证每个方法只能同时有一个线程进入,配合wait和notifyall方法,可以启动或者唤醒线程。
方法二:通过主线程Join()
class T11 extends Thread {
public void run() {
System.out.println("in T1");
}
}
class T22 extends Thread {
public void run() {
System.out.println("in T2");
}
}
class T33 extends Thread {
public void run() {
System.out.println("in T3");
}
}
public class Test2 {
public static void main(String[] args) throws InterruptedException {
T11 t1 = new T11();
T22 t2 = new T22();
T33 t3 = new T33();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
}
方法三:通过线程执行时Join()
class T1 extends Thread {
public void run(){
Random random = new Random();
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("in T1");
}
}
class T2 extends Thread{
private Thread thread;
public T2(Thread thread) {
this.thread = thread;
}
public void run(){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("in T2");
}
}
class T3 extends Thread{
private Thread thread;
public T3(Thread thread) {
this.thread = thread;
}
public void run(){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("in T3");
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
T1 t1 = new T1();
T2 t2 = new T2(t1);
T3 t3 = new T3(t2);
t2.start();
t1.start();
t3.start();
}
}
更多java相关内容感兴趣的读者可查看本站专题:《Java进程与线程操作技巧总结》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/difffate/article/details/63684290
0
投稿
猜你喜欢
- 前言本文主要介绍了关于java静默加载Class的相关内容,之所以有这篇文章,是因为有时候在开发的时候,我们有这样的场景,我们只想得到一个C
- 原因:feign传值出错无法接收到传值由于是POST所以添加@RequestBody进行尝试解决:错误原因是未添加@RequestBody尝
- 1、volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)&nb
- MyBatis 是一款常用的持久层框架,使得程序能够以调用方法的方式执行某个指定的SQL,将执行SQL的底层逻辑进行封装。多数与Spring
- 写在前面关于数据结构,Java官方其实已经帮我们写好并封装起来了,在真正需要使用的时候直接调用即可,但为了更好的理解数据结构,我会按照源码的
- 1、安装依赖<dependency> <
- 1 关于自动内存管理Java是由jvm来管理内存,包括自动分配以及自动回收,因此它不容易出现内存泄漏和内存溢出问题。C/C++,由程序员手动
- 一,项目简介经过调查研究进行开发设计的这款仓库管理系统,主要是为商家提供商品货物进销存的信息化管理,以便让商家在竞争如此激烈的今天占据一定的
- 一、什么是CharacterEncodingFilter官方解释如下是spring内置过滤器的一种,用来指定请求或者响应的编码格式。在web
- 本文为大家分享了java画出五子棋游戏棋盘的方法,供大家参考,具体内容如下棋盘模块:画五子棋棋盘:19条横线、19条竖线步骤一:显示棋盘我有
- 前言java处理excel转pdf一直没找到什么好用的免费jar包工具,自己手写的难度,恐怕高级程序员花费一年的事件,也不能做出来非常好用,
- 1集合的概念把集合看做是一个容器,集合不是一个类,是一套集合框架,框架体系包含很多的集合类,java api提供了集合存储任意类型(基本包装
- ApplicationContext简述ApplicationContext代表IOC容器,在SpringIOC容器中读取Bean配置创建B
- 本文实例讲述了Java简单验证身份证功能。分享给大家供大家参考,具体如下:package org.cxy.csdn.example;impo
- 软引用简介软引用是用来表示某个引用会被GC(垃圾处理器)收集的类。当有引用指向某个obj的时候,通常发生GC的时候不会把这个对象处理掉,但是
- import java.awt.AlphaComposite;import java.awt.Color;import java.awt.F
- 这篇文章主要介绍了Springboot整合Shiro的代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 借用@Caching实现入参是基本类型的:@Caching(evict={@CacheEvict(value = Cache.CONSTAN
- 什么是代理模式?代理模式:在调用处不直接调用目标类进行操作,而是调用代理类,然后通过代理类来调用目标类进行操作。在代理类调用目标类的前后可以
- 上篇文章中我们介绍了浅谈Spring的两种配置容器,接下来我们就了解下spring中的FactoryBean的相关内容,具体如下。从Sess