实现并发线程按顺序输出123

转自:https://blog.csdn.net/a13662080711/article/details/79490631

问题:
有first,second,third,forth四个线程,first输出1,second输出2,third输出3,forth输出4。要求, 同时启动四个线程, 按顺序输出1234, 且未无限循环输出。

这是一个多线程协同的问题,本身多线程是没有执行顺序的, 顺序不一定。
但是可以采取一定方式使多线程按一定顺序执行。

ReentrantLock

ReentrantLock来解决, 还有个state整数用来判断轮到谁执行了。

public class PrintTwo {
    private static Lock lock = new ReentrantLock();//通过JDK5中的锁来保证线程的访问的互斥
    private static int state = 0; 
    static class First extends Thread {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                if (state % 4 == 0) {
                    System.out.println("1");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    static class Second extends Thread {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                if (state % 4 == 1) {
                    System.out.println("2");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    static class Third extends Thread {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                if (state % 4 == 2) {
                    System.out.println("3");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    static class Forth extends Thread {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                if (state % 4 == 3) {
                    System.out.println("4");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        First first = new First();
        Second second = new Second();
        Third third = new Third();
        Forth forth = new Forth();
        first.start();
        second.start();
        third.start();
        forth.start();
     }     
}

Semphore

Semphore,完成对信号量的控制,可以控制某个资源可以被同时访问的个数,通过acquire()获取一个许可,如果没有就等待,而release()释放一个许可。(另外,semphore还可以用来控制同时访问一个方法的线程数:https://blog.csdn.net/anhenzhufeng/article/details/70225415 )

package Thread;

import java.util.concurrent.Semaphore;

public class Print1234 {
    public static Semaphore sem1;
    public static Semaphore sem2;
    public static Semaphore sem3;
    public static Semaphore sem4;

    static class FirstThread extends Thread {
        public void run() {
            try {
                while (true) {
                    sem1.acquire();
                    System.out.println("1");
                    sem2.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class SecondThread extends Thread {
        public void run() {
            try {
                while (true) {
                    sem2.acquire();
                    System.out.println("2");
                    sem3.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class ThirdThread extends Thread {
        public void run() {
            try {
                while (true) {
                    sem3.acquire();
                    System.out.println("3");
                    sem4.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class ForthThread extends Thread {
        public void run() {
            try {
                while (true) {
                    sem4.acquire();
                    System.out.println("4");
                    sem1.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        sem1 = new Semaphore(1);
        sem2 = new Semaphore(1);
        sem3 = new Semaphore(1);
        sem4 = new Semaphore(1);
        try {
            // 不要有sem1.acquire()
            sem2.acquire();
            sem3.acquire();
            sem4.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new FirstThread().start();
        new SecondThread().start();
        new ThirdThread().start();
        new ForthThread().start();

    }
}

volatile

volatile,保证被修饰的变量在读写前都会与主存交互更新。一个变量被volatile修饰后,则不同线程对这个变量进行操作时,总是从内存中读取最新值,即每次更新对其他线程都是立即可见的。

public class PrintThree {
    volatile static int state = 0;

    static class First extends Thread {
        @Override
        public void run() {
            while (true) {
                if (state % 4 == 0) {
                    System.out.println("1");
                    state++;
                }
            }
        }
    }

    static class Second extends Thread {
        @Override
        public void run() {
            while (true) {
                if (state % 4 == 1) {
                    System.out.println("2");
                    state++;
                }
            }
        }
    }

    static class Third extends Thread {
        @Override
        public void run() {
            while (true) {
                if (state % 4 == 2) {
                    System.out.println("3");
                    state++;
                }
            }
        }
    }

    static class Forth extends Thread {
        @Override
        public void run() {
            while (true) {
                if (state % 4 == 3) {
                    System.out.println("4");
                    state++;
                }
            }
        }
    }

    public static void main(String[] args) {
        First first = new First();
        Second second = new Second();
        Third third = new Third();
        Forth forth = new Forth();
        first.start();
        second.start();
        third.start();
        forth.start();
    }
}

猜你喜欢

转载自blog.csdn.net/anhenzhufeng/article/details/80348400