多线程交替输出1234

1、synchronized

复制代码
package threadT;

public class TestPrint {
    static int i = 1;

    public static void main(String[] args) throws Exception {
        final Object obj = new Object();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        synchronized (obj) {
                            Thread.sleep(1000);
                            obj.notifyAll();
                            obj.wait();
                            if (i == 1)
                                System.out.println(i++ % 3);
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        synchronized (obj) {
                            Thread.sleep(1000);
                            obj.notifyAll();
                            obj.wait();
                            if (i == 2)
                                System.out.println(i++ % 3);
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        Thread t3 = new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        synchronized (obj) {
                            Thread.sleep(1000);
                            obj.notifyAll();
                            obj.wait();
                            if (i == 3) {
                                System.out.println(3);
                                i = 1;
                            }
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    };
}
复制代码

2、ReentrantLock

复制代码
package threadT;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestPrint {
    private static Lock lock = new ReentrantLock();
    private static int state = 0;

    static class First extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.lock();
                if (state % 4 == 0) {
                    System.out.println("1");
                    state++;
                }
                lock.unlock();
            }
        }
    }

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

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

    static class Forth extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                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();
    }
}
复制代码

3、Semaphore

复制代码
package threadT;

import java.util.concurrent.Semaphore;

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

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

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

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

    static class ForthThread extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(1000);
                    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();

    }
}
复制代码

4、volatile

复制代码
package threadT;

public class TestPrint {
    volatile static int state = 0;

    static class First extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (state % 4 == 0) {
                    System.out.println("1");
                    state++;
                }
            }
        }
    }

    static class Second extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (state % 4 == 1) {
                    System.out.println("2");
                    state++;
                }
            }
        }
    }

    static class Third extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (state % 4 == 2) {
                    System.out.println("3");
                    state++;
                }
            }
        }
    }

    static class Forth extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                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();
    }
}
复制代码

 

posted @   wujf  阅读(884)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示