java指定方法的执行顺序_Java 指定线程执行顺序(三种方式)

Java 指定线程执行顺序(三种方式)

0 阅读

作者:sunwenlong1

来源:CSDN

2017-10-17

博主地址:http://blog.csdn.net/difffate

*方法二 *方法三

方法一:通过共享对象锁加上可见变量来实现。

[java] view plaincopy

print?publicclassMyService {

privatevolatileintorderNum =1;

publicsynchronizedvoidmethodA() {

try{

while(orderNum !=1) {

}

for(inti =0; i <2; i++) {

System.out.println(”AAAAA”);

orderNum = 2;

} catch(InterruptedException e) {

}

publicsynchronizedvoidmethodB() {

try{

while(orderNum !=2) {

wait();

for(inti =0; i <2; i++) {

”BBBBB”);

}

3;

notifyAll();

catch(InterruptedException e) {

e.printStackTrace();

}

publicsynchronizedvoidmethodC() {

try{

while(orderNum !=3) {

}

for(inti =0; i <2; i++) {

System.out.println(”CCCCC”);

orderNum = 1;

} catch(InterruptedException e) {

}

}

Fimh25QdUrcIPsB8sKFFaapkGB3Spublic 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 plaincopy

print?importservice.MyService;

publicclassThreadAAextendsThread {

privateMyService dbtools;

publicThreadAA(MyService dbtools) {

super();

this.dbtools = dbtools;

}

@Override

publicvoidrun() {

dbtools.methodA();

Fimh25QdUrcIPsB8sKFFaapkGB3Simport 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 plaincopy

print?importservice.MyService;

publicclassThreadBBextendsThread {

privateMyService dbtools;

publicThreadBB(MyService dbtools) {

super();

this.dbtools = dbtools;

}

@Override

publicvoidrun() {

dbtools.methodB();

Fimh25QdUrcIPsB8sKFFaapkGB3Simport 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 plaincopy

print?importservice.MyService;

publicclassThreadCCextendsThread {

privateMyService dbtools;

publicThreadCC(MyService dbtools) {

this.dbtools = dbtools;

@Override

publicvoidrun() {

}

}

Fimh25QdUrcIPsB8sKFFaapkGB3Simport 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 plaincopy

print?importextthread.ThreadCC;

importservice.MyService;

importextthread.ThreadAA;

importextthread.ThreadBB;

publicclassRun {

publicstaticvoidmain(String[] args) {

MyService myService = newMyService();

for(inti =0; i <2; i++) {

ThreadBB output = newThreadBB(myService);

ThreadAA input = newThreadAA(myService);

ThreadCC threadCC = newThreadCC(myService);

}

Fimh25QdUrcIPsB8sKFFaapkGB3Simport 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();

}

}

}

执行结果:

Fj_9GxTB0udPz7L_MSPs_IrobaQr

可以看到线程的启动按顺序执行了。共享对象锁,可以保证每个方法只能同时有一个线程进入,配合wait和notifyall方法,可以启动或者唤醒线程。

方法二:通过主线程Join()

[java] view plaincopy

print?classT11extendsThread {

publicvoidrun() {

System.out.println(”in T1”);

}

classT22extendsThread {

publicvoidrun() {

System.out.println(”in T2”);

}

classT33extendsThread {

publicvoidrun() {

System.out.println(”in T3”);

}

publicclassTest2 {

publicstaticvoidmain(String[] args)throwsInterruptedException {

T11 t1 = newT11();

newT22();

T33 t3 = newT33();

t1.join();

t2.join();

}

Fimh25QdUrcIPsB8sKFFaapkGB3Sclass 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()

[java] view plaincopy

print?classT1extendsThread {

publicvoidrun(){

Random random = newRandom();

try{

Thread.sleep(random.nextInt(1000));

catch(InterruptedException e) {

e.printStackTrace();

System.out.println(”in T1”);

}

classT2extendsThread{

privateThread thread;

publicT2(Thread thread) {

this.thread = thread;

}

publicvoidrun(){

try{

thread.join();

catch(InterruptedException e) {

e.printStackTrace();

System.out.println(”in T2”);

}

classT3extendsThread{

privateThread thread;

publicT3(Thread thread) {

this.thread = thread;

}

publicvoidrun(){

try{

thread.join();

catch(InterruptedException e) {

e.printStackTrace();

System.out.println(”in T3”);

}

publicclassTest {

publicstaticvoidmain(String[] args)throwsInterruptedException {

T1 t1 = newT1();

newT2(t1);

T3 t3 = newT3(t2);

t1.start();

}

Fimh25QdUrcIPsB8sKFFaapkGB3Sclass 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();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值