java 保证线程顺序的几种方式
2018-02-06 15:44
260 查看
方法一:通过共享对象锁加上可见变量来实现。
[java] view
plain copy
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();
}
}
}
[java] view
plain copy
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();
}
}
[java] view
plain copy
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();
}
}
[java] view
plain copy
import service.MyService;
public class ThreadCC extends Thread {
private MyService dbtools;
public ThreadCC(MyService dbtools) {
this.dbtools = dbtools;
}
@Override
public void run() {
dbtools.methodC();
}
}
[java] view
plain copy
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();
}
}
}
执行结果:
[java] view
plain copy
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();
}
}
}
[java] view
plain copy
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();
}
}
[java] view
plain copy
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();
}
}
[java] view
plain copy
import service.MyService;
public class ThreadCC extends Thread {
private MyService dbtools;
public ThreadCC(MyService dbtools) {
this.dbtools = dbtools;
}
@Override
public void run() {
dbtools.methodC();
}
}
[java] view
plain copy
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();
}
}
}
执行结果:
相关文章推荐
- Java 指定线程执行顺序(三种方式)
- java中创建线程的的几种方式
- java 线程间通信的几种方式
- JAVA 使用线程的几种方式
- Java多线程有哪几种实现方式? Java中的类如何保证线程安全? 请说明ThreadLocal的用法和适用场景
- Java 指定线程执行顺序(三种方式)
- java 多线程 停止线程几种方式
- java线程的几种方式
- Java线程同步的几种方式
- Java 多线程 (PART VII) 使用join()方法保证线程的执行顺序
- java间线程通信的几种方式(II)
- Java复习:创建线程的的几种方式
- [面试]java创建线程有几种方式?
- JAVA线程间通信的几种方式
- JAVA线程间通信的几种方式
- java停止线程的几种方式
- java 创建线程的几种方式
- 创建线程有几种方式和Java中常用的线程池
- JAVA线程间通信的几种方式
- Java中创建线程的几种方式以及线程同步的几种方式