多线程实现顺序执行

方式1:
 

package com.ultrapower.demo1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class T611 extends Thread {
    public void run() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("in T1");
    }
}

class T622 extends Thread {
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("in T2");
    }
}

class T633 extends Thread {
    public void run() {
        System.out.println("in T3");
    }
}

public class Demo6 {
    public static void main(String[] args) {
        T611 t1 = new T611();
        T622 t2 = new T622();
        T633 t3 = new T633();
        ExecutorService single = Executors.newSingleThreadExecutor();
        single.submit(t1);
        single.submit(t2);
        single.submit(t3);
        single.shutdown();
    }

}

方式二:

package com.ultrapower.demo1;

class T11 extends Thread {
    public void run() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("in T1");
    }
}
 
class T22 extends Thread {
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("in T2");
    }
}
 
class T33 extends Thread {
    public void run() {
        System.out.println("in T3");
    }
}
public class Demo5 {
    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();
    } 
}

方式三:

package com.ultrapower.demo1;

/**
 * 实现线程的顺序启动
 * 
 * @author Administrator
 *
 */
public class Demo2 {
    public static void main(String[] args) throws Exception {
        final Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread name "+Thread.currentThread().getName());
            }

        },"a");
        final Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                
                try {
                    t1.join();
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread name "+Thread.currentThread().getName());
            }
            
        },"b");
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t2.join();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread name "+Thread.currentThread().getName());
            }
            
        },"c");
        
        t1.start();
        t2.start();
        t3.start();
        
    
        System.out.println("main end ");
    }
}


 

方式4:通过共享锁的方式实现

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();
        }
    }
}
public class ThreadAA extends Thread {
 
    private MyService dbtools;
 
    public ThreadAA(MyService dbtools) {
        super();
        this.dbtools = dbtools;
    }
 
    @Override
    public void run() {
        dbtools.methodA();
    }
 
}
public class ThreadBB extends Thread {
 
    private MyService dbtools;
 
    public ThreadBB(MyService dbtools) {
        super();
        this.dbtools = dbtools;
    }
 
    @Override
    public void run() {
        dbtools.methodB();
    }
 
}


public class ThreadCC extends Thread {
 
    private MyService dbtools;
 
    public ThreadCC(MyService dbtools) {
        this.dbtools = dbtools;
    }
 
    @Override
    public void run() {
        dbtools.methodC();
    }
}

测试:

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

多线程实现任务的顺序执行

public class MyServiceDemo2 {
   // 这段代码主要是保证三个线程取的数据必须按照 A 1,4,7....B 2,5,8....C 3,6,9...
   // 的顺序进行取数据

    private int[] data = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    private volatile int index = 0;
    private volatile int flag = 0;

    public synchronized int getMethod1() {
        int result = -1;
        try {
            while (flag != 0) {
                wait();
            }
            System.out.println("有线程进来获取数据 threadName :" + Thread.currentThread().getName());
            if (index < data.length) {
                result = data[index];
            }
            ++index;
            flag = 1;
            notifyAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return result;
    }

    public synchronized int getMethod2() {

        int result = -1;
        try {
            while (flag != 1) {
                wait();
            }
            System.out.println("有线程进来获取数据 threadName :" + Thread.currentThread().getName());
            if (index < data.length) {
                result = data[index];
            }
            ++index;
            flag = 2;
            notifyAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return result;
    }

    public synchronized int getMethod3() {

        int result = -1;
        try {
            while (flag != 2) {
                wait();
            }
            System.out.println("有线程进来获取数据 threadName :" + Thread.currentThread().getName());
            if (index < data.length) {
                result = data[index];
            }
            ++index;
            flag = 0;
            notifyAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return result;
    }
}

  创建线程对象

public class MyThreadDemo {

    public static void main(String[] args) {
        MyServiceDemo2 serviceDemo = new MyServiceDemo2();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    // 这里加锁是为了保存同步方法外的执行顺序
                    synchronized (serviceDemo) {
                        int method1 = serviceDemo.getMethod1();
                        if (method1 < 0) {
                            break;
                        }
                        try {
                            Thread.sleep(1000L);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("A data:" + method1);
                    }
                }
            }
        }, "A");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {
                    synchronized (serviceDemo) {
                        int method1 = serviceDemo.getMethod2();
                        if (method1 < 0) {
                            break;
                        }
                        System.out.println("B data:" + method1);
                    }
                }
            }
        }, "B");

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {
                    synchronized (serviceDemo) {
                        int method1 = serviceDemo.getMethod3();
                        if (method1 < 0) {
                            break;
                        }
                        System.out.println("C data:" + method1);
                    }
                }
            }
        }, "C");

        t1.start();
        t2.start();
        t3.start();
    }
}

其他的省略,脑补一下吧

测试结果:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值