这个是任务类,可以申请资源,有一个run方法,是子类实现的 import java.util.concurrent.Semaphore; /** * 抽象任务,具体的执行任务,归实现类负责 * * @author Administrator * */ public abstract class Task { public abstract void run(); private Semaphore s; private boolean hasExisted = false; public void P(final Semaphore s) throws InterruptedException { if (s == null) { // 申请空的信号量 throw new InterruptedException("不能为空"); } if (hasExisted) {// 已经申请了一个资源,还没有释放 throw new InterruptedException("已经占用一个资源"); } s.acquire();// 阻塞 this.s = s; hasExisted = true; } public boolean V() { if (!hasExisted) { return false;// 没沾有资源就不能说释放了 } s.release();//释放资源 hasExisted = false; s = null; return true; } } 这个是互斥对象,用于实现互斥 import java.util.concurrent.Semaphore; /** * 用信号量实现互斥 * * @author Administrator * */ public class Mutex extends Semaphore { private static final long serialVersionUID = 1L; public Mutex() { super(1);//互斥是1 } } 测试类 import java.util.concurrent.Semaphore; public class Test { public static void main(String[] args) { // final Semaphore s = new Semaphore(2);//信号量,执行结果run the task1 run the // task2 run the task3 final Semaphore s = new Mutex();// 互斥 run the task1 run the task2 run // the task3 new Thread() { public void run() { Task1 task2 = new Task1(); try { task2.P(s);// 申请资源 task2.run();// 执行任务 task2.V();// 释放资源 } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); new Thread() { public void run() { Task2 task2 = new Task2(); try { task2.P(s); task2.run(); task2.V(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); new Thread() { public void run() { Task3 task2 = new Task3(); try { task2.P(s); task2.run(); task2.V(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } private static class Task1 extends Task { public void run() { System.out.println("run the task1"); try { Thread.sleep(3000);// 停顿 } catch (InterruptedException e) { e.printStackTrace(); } } } private static class Task2 extends Task { public void run() { System.out.println("run the task2"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } private static class Task3 extends Task { public void run() { System.out.println("run the task3"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } C++版 #include<windows.h> #include<iostream.h> DWORD WINAPI Thread1Proc( LPVOID lpParameter ); DWORD WINAPI Thread2Proc( LPVOID lpParameter ); int index=0; int tickets=100; HANDLE hMutex; void main() { HANDLE hThread1; HANDLE hThread2; hThread1=CreateThread(NULL,0,Thread1Proc,NULL,0,NULL); hThread2=CreateThread(NULL,0,Thread2Proc,NULL,0,NULL); CloseHandle(hThread1);//关闭句柄, 此时系统会递减新线程的线程内核对象的使用计数, 当创建的线程 CloseHandle(hThread2);//执行完毕后, 系统也会递减线程内核对象的使用计数, 计数为0时, 系统释放线程内核对象 hMutex=CreateMutex(NULL,TRUE,NULL); //TRUE表示调用CreateMutex的线程拥有了互斥对象 WaitForSingleObject(hMutex,INFINITE);//线程多次拥有互斥对象, 主要通过互斥对象内的计数器实现 ReleaseMutex(hMutex); //申请两次则要释放两次 ReleaseMutex(hMutex); //谁申请谁释放 Sleep(1000); //要保证在卖完100张票前,主线程不能退出 } DWORD WINAPI Thread1Proc( LPVOID lpParameter ) { while(TRUE) //保证线程的不断执行 { WaitForSingleObject(hMutex,INFINITE); //获取信号量 if(tickets>0) { Sleep(1); cout<<"thread1 sells ticket: "<<tickets--<<endl; } else break; ReleaseMutex(hMutex); //释放信号量 } return 0; //WaitForSingleObject(hMutex,INFINITE); //cout<<"thread1 is running! "<<endl; //return 0; } DWORD WINAPI Thread2Proc( LPVOID lpParameter ) { while(TRUE) { WaitForSingleObject(hMutex,INFINITE); if(tickets>0) { Sleep(2); cout<<"thread2 sells ticket: "<<tickets--<<endl; } else break; ReleaseMutex(hMutex); } return 0; //WaitForSingleObject(hMutex,INFINITE); //cout<<"thread2 is running! "<<endl; //return 0; }