zoukankan      html  css  js  c++  java
  • 三个线程交替打印1到100

    public class Test {
        volatile int i = 1;
    
        public static void main(String[] args) throws Exception {
            Test obj = new Test();
    
    
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    while (obj.i <= 100) {
                        // 上锁当前对象
                        synchronized (this) {
    
                            // 唤醒另一个线程
                            notifyAll();
                            if (obj.i == 101) {
                                return;
                            }
    
                            int i = new Integer(Thread.currentThread().getName());
                            if (obj.i % 3 == i) {
                                System.out.println("Thread " + Thread.currentThread().getName() + " " + obj.i++);
                            }
                            if(obj.i % 3==1){
                                System.out.println("==========");
                            }
                            try {
                                if (obj.i == 101) {
                                    notifyAll();
                                    return;
                                } else {
                                    // 释放掉锁
                                    wait();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            };
    
            // 启动多个线程(想创建几个就创建几个)
            Thread t1 = new Thread(runnable);
            Thread t2 = new Thread(runnable);
            Thread t3 = new Thread(runnable);
    
            t1.setName("1");
            t2.setName("2");
            t3.setName("0");
            t1.start();
            t2.start();
            t3.start();
    
    
        }
    
    
    }
  • 相关阅读:
    Directory类的使用、Alt+Shift+F10可以查看其命名空间
    用户控件
    图像检测算法Halcon 10的使用
    MD5加密的使用
    AppDomain.CurrentDomain.AssemblyResolve
    记事本程序
    C#文件操作
    部分常用控件
    TreeView的使用
    ComboBox的使用
  • 原文地址:https://www.cnblogs.com/zzq-include/p/13527151.html
Copyright © 2011-2022 走看看