private static char c = &#39;A&#39;; private static int i = 0; public static void main(String[] args) { Runnable runnable = new Runnable() { public void run() { synchronized (this) {//加锁 try { int threadId = Integer.parseInt(Thread.currentThread().getName()); while (i < 26) { if (i % 3 == threadId - 1) { System.out.println(threadId +""+ (char) c++); i++; notifyAll();// 唤醒处于等待状态的线程 } else { wait();// 释放当前锁并进入等待状态 } } } catch (InterruptedException e) { e.printStackTrace(); } }//执行结束释放当前锁 } }; Thread t1 = new Thread(runnable, "1"); t1.start(); t2.start(); t3.start(); }
点赞 评论

相关推荐

牛客77743221...:做一段时间,公司出钱送你去缅甸和泰国旅游
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务