import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//一个线程去读取List的长度,如果List为空的时候,线程就wait,如果List中有值了,就notify去获取它的长度
public class ListAwit {
private static List<Integer> list = new ArrayList<Integer>();
final static Random rand = new Random();
public static void main(String[] args) {
final ListAwit la = new ListAwit();
new Thread(new Runnable(){
public void run() {
while(true){
if(list.size() > 0){
synchronized(la){
la.notify();
}
}
sleep();
}
}})
.start();
while(true){
try {
System.out.println(Thread.currentThread().getName()+"等待.........");
synchronized(la){
la.wait();
}
sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"##长度:"+list.size());
System.out.println(list);
}
}
private static void sleep(){
try {
int randint = rand.nextInt(2000);
Thread.sleep(randint);
list.add(randint);
if(rand.nextInt(10)==5){
list.clear();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 运行结果:
main等待.........
main##长度:4
[289, 887, 41, 1192]
main等待.........
main##长度:8
[289, 887, 41, 1192, 1322, 238, 641, 1608]
main等待.........
*/
}
实现一种类似信号控制的程序
public class SynchronizedMutex {
private Thread curOwner = null;
public synchronized void acquire() throws InterruptedException{
System.out.println("当前线程:"+Thread.currentThread().getName());
while(curOwner != null){
this.wait();
}
this.curOwner = Thread.currentThread();
}
public synchronized void release() throws InterruptedException{
System.out.println("当前线程:"+Thread.currentThread().getName());
while(curOwner == Thread.currentThread()){
this.curOwner = null;
this.notify();
}
}
}
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class MutexTest{
// private Semaphore mutex = new Semaphore(1);
private SynchronizedMutex mutex = new SynchronizedMutex();
public static void main(String[] args) {
final MutexTest test = new MutexTest();
Runnable task = new Runnable(){
@Override
public void run() {
while(true){
int i = new Random().nextInt(3);
switch(i){
case 0:test.add();
case 1:test.add1();
case 2:test.del();
default:test.add();
}
}
}
};
ExecutorService exec = Executors.newCachedThreadPool();
for(int i=0; i<5; i++){
exec.execute(task);
}
}
private static int value;
public void add(){
try {
mutex.acquire();
TimeUnit.SECONDS.sleep(1);
System.out.println(Thread.currentThread().getName()+"增一:"+ (++value));
mutex.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void add1(){
try {
mutex.acquire();
TimeUnit.SECONDS.sleep(1);
value = value + 2;
System.out.println(Thread.currentThread().getName()+"增二:"+ value);
mutex.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void del(){
try {
mutex.acquire();
TimeUnit.SECONDS.sleep(1);
System.out.println(Thread.currentThread().getName()+"减一:"+ (--value));
mutex.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 运行结果:
* 当前线程:pool-1-thread-2
当前线程:pool-1-thread-4
当前线程:pool-1-thread-5
当前线程:pool-1-thread-3
当前线程:pool-1-thread-1
pool-1-thread-2减一:-1
当前线程:pool-1-thread-2
当前线程:pool-1-thread-2
pool-1-thread-4增一:0
当前线程:pool-1-thread-4
当前线程:pool-1-thread-4
pool-1-thread-4增二:2
当前线程:pool-1-thread-4
当前线程:pool-1-thread-4
pool-1-thread-4减一:1
当前线程:pool-1-thread-4
当前线程:pool-1-thread-4
pool-1-thread-4增一:2
当前线程:pool-1-thread-4
当前线程:pool-1-thread-4
pool-1-thread-4减一:1
当前线程:pool-1-thread-4
当前线程:pool-1-thread-4
*/
}