消费者-生产者模型
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.Semaphore;
public class ProductAndCustomer {
private LinkedList<Integer> list = new LinkedList<>();
private Semaphore product = new Semaphore(3);
private Semaphore customer = new Semaphore(0);
public void put(int v) throws InterruptedException {
product.acquire();
synchronized (list) {
list.add(v);
}
customer.release();
}
public int get() throws InterruptedException {
int ret = -1;
customer.acquire();
synchronized (list) {
ret = list.pop();
}
product.release();
return ret;
}
public static void main(String[] args) {
final ProductAndCustomer productAndCustomer = new ProductAndCustomer();
for(int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Random ran = new Random();
while(true) {
try {
int nextInt = ran.nextInt(10);
productAndCustomer.put(nextInt);
System.out.println(Thread.currentThread().getName() + "=>" + nextInt);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"product-" + i).start();;
}
for(int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Random ran = new Random();
while(true) {
try {
System.out.println(Thread.currentThread().getName() + "=>" + productAndCustomer.get());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"customer-" + i).start();;
}
}
}
自定义线程池
import java.util.concurrent.LinkedBlockingQueue;
public class MyThreadPools {
private LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
public MyThreadPools(int size) {
if(size < 1) {
throw new RuntimeException("线程池大小至少为1");
}
for(int i = 0; i < size; i++) {
new Thread(new ThreadTask(queue),"my-pool-" + i).start();
}
}
public MyThreadPools(int size, String name) {
if(size < 1) {
throw new RuntimeException("线程池大小至少为1");
}
for(int i = 0; i < size; i++) {
new Thread(new ThreadTask(queue), name + i).start();
}
}
public void add(Runnable run) {
queue.add(run);
}
class ThreadTask implements Runnable{
private LinkedBlockingQueue<Runnable> queue = null;
ThreadTask(LinkedBlockingQueue<Runnable> queue){
this.queue = queue;
}
@Override
public void run() {
Runnable run = null;
while(true) {
try {
run = queue.take();
run.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
MyThreadPools pool = new MyThreadPools(3);
for(int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
} catch (Exception e) {
}
}
});
pool.add(thread);
}
}
}
自定义定时器
import java.util.Random;
import java.util.concurrent.PriorityBlockingQueue;
public class MyTimer {
private PriorityBlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();
private Object lock = new Object();
public void schdule(Runnable run, long time) {
MyTask myTask = new MyTask(run, time);
queue.add(myTask);
synchronized (lock) {
lock.notifyAll();
}
}
public MyTimer() {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
try {
MyTask myTask = queue.take();
long time = myTask.getTime();
long curr = System.currentTimeMillis();
if(curr < time) {
queue.add(myTask);
synchronized (lock) {
lock.wait(time - curr);
}
continue;
}
myTask.run();
} catch (Exception e) {
}
}
}
}).start();
}
class MyTask implements Comparable<MyTask>{
private Runnable run;
private long time;
MyTask(Runnable run, long time){
this.run = run;
this.time = time + System.currentTimeMillis();
}
@Override
public int compareTo(MyTask o) {
return (int)(this.time - o.time);
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public void run() {
run.run();
}
}
public static void main(String[] args) {
MyTimer myTimer = new MyTimer();
Random ran = new Random();
for(int i = 0; i < 5; i++) {
long time = ran.nextInt(10) * 1000;
System.out.println(time);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
myTimer.schdule(thread, time);
}
}
}
自定义读写锁
public class ReadAndWrite {
private int reads = 0;
private int writes = 0;
private int writeRequests = 0;
private Thread currThread = null;
public synchronized void readLock() {
try {
while(writes > 0 || writeRequests > 0) {
wait();
}
reads++;
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void readUnlock() {
reads--;
notifyAll();
}
public synchronized void writeLock() {
Thread t = Thread.currentThread();
writeRequests++;
try {
while(reads > 0 || (writes > 0 && currThread != t)) {
wait();
}
if(currThread == null) {
currThread = t;
}
writeRequests--;
writes++;
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void writeUnlock() {
writes--;
notifyAll();
}
static int j = 0;
public static void main(String[] args) {
final ReadAndWrite readAndWrite = new ReadAndWrite();
readAndWrite.writeLock();
readAndWrite.writeLock();
System.out.println("===================");
readAndWrite.writeUnlock();
readAndWrite.writeUnlock();
for(int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
readAndWrite.writeLock();
try {
j++;
System.out.println(Thread.currentThread().getName() + "=> " + j);
} catch (Exception e) {
}finally {
readAndWrite.writeUnlock();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"write-" + i).start();
}
for(int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
readAndWrite.readLock();
try {
System.out.println(Thread.currentThread().getName() + "=> " + j);
} catch (Exception e) {
}finally {
readAndWrite.readUnlock();
}
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"read-" + i).start();
}
}
}