package com.gxx.test; /** * 线程 */ public class MyThread extends Thread { int index;//索引 LockObject lockObject;//锁对象 int threadCount;//线程总数 /** * 构造方法 * @param index * @param lockObject * @param threadCount */ public MyThread(int index, LockObject lockObject, int threadCount) { this.index = index; this.lockObject = lockObject; this.threadCount = threadCount; } @Override public void run() { /** * 锁,只有一个线程进入 */ synchronized (lockObject) { while (true) { try { if (lockObject.count % threadCount == index) { System.out.println((lockObject.count+1) + ","); Thread.sleep(100); lockObject.count ++; /** * 唤起wait的线程 */ lockObject.notifyAll(); } else { /** * 释放锁 */ lockObject.wait(); } } catch (Exception e) { e.printStackTrace(); } } } } }