您的位置:首页 > 编程语言 > Java开发

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

        }  

    }  

  

}  

执行结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: