1、使用传统方式
package com.example.demo.jucpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
/**
* while(true)形式 持续监听
*
* @author zl
*
*/
public class CorePoolDemo {
public static void main(String[] args) {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(100), threadFactory, new ThreadPoolExecutor.AbortPolicy());
Container container = new Container();
poolExecutor.execute(new MyThread12(container));
poolExecutor.execute(new MyThread11(container));
poolExecutor.shutdown();
}
}
class MyThread11 implements Runnable {
private Container container;
public MyThread11(Container container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(500);
System.out.println(Thread.currentThread().getName() + " t1增加:" + i);
container.addNumber(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyThread12 implements Runnable {
private Container container;
public MyThread12(Container container) {
this.container = container;
}
@Override
public void run() {
System.out.println("t2开始");
for (;;) {
try {
TimeUnit.NANOSECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
Integer size = container.getSize();
if (size >= 5) {
System.out.println(Thread.currentThread().getName() + " t2结束========--");
break;
}
}
}
}
需要持续消耗cpu资源
2、wait/notify加同步代码块形式
package com.example.demo.jucpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
/**
* wait/notify-notifyall 实现,需要synchronized加持
*
* @author zl
*
*/
public class CorePoolDemo1 {
public static void main(String[] args) {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(100), threadFactory, new ThreadPoolExecutor.AbortPolicy());
Container container = new Container();
Object lockObject = new Object();
poolExecutor.execute(new MyThread21(container, lockObject));
poolExecutor.execute(new MyThread22(container, lockObject));
poolExecutor.shutdown();
}
}
class MyThread21 implements Runnable {
private Container container;
private Object lockObject;
public MyThread21(Container container, Object lockObject) {
this.container = container;
this.lockObject = lockObject;
}
@Override
public void run() {
synchronized (lockObject) {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " t1增加:" + i);
container.addNumber(i);
if (container.getSize() == 5) {
try {
lockObject.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class MyThread22 implements Runnable {
private Container container;
private Object lockObject;
public MyThread22(Container container, Object lockObject) {
this.container = container;
this.lockObject = lockObject;
}
@Override
public void run() {
System.out.println("t2开始");
synchronized (lockObject) {
System.out.println(Thread.currentThread().getName() + " t2结束========--");
lockObject.notify();
}
}
}
3、使用LockSupport实现
package com.example.demo.jucpool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
/**
* LockSupport实现
*
* @author zl
*
*/
public class CorePoolDemo2 {
private static Container container = new Container();
static Thread t1 = null;
static Thread t2 = null;
public static void main(String[] args) {
t1 = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " t1增加:" + i);
container.addNumber(i);
if (container.getSize() == 5) {
LockSupport.unpark(t2);
LockSupport.park();
}
}
}, "t1");
t2 = new Thread(() -> {
System.out.println("t2开始");
LockSupport.park();
System.out.println(Thread.currentThread().getName() + " t2结束========--");
LockSupport.unpark(t1);
}, "t2");
t2.start();
t1.start();
}
}
4、 信号量 seamphore实现
package com.example.demo.jucpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
/**
* 信号量 seamphore实现
*
* @author zl
*
*/
public class CorePoolDemo3 {
public static void main(String[] args) {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(100), threadFactory, new ThreadPoolExecutor.AbortPolicy());
Container container = new Container();
Semaphore semaphore = new Semaphore(1);
poolExecutor.execute(new Mythread41(semaphore, container));
poolExecutor.execute(new Mythread42(semaphore, container));
poolExecutor.shutdown();
}
}
class Mythread41 implements Runnable {
private Semaphore semaphore;
private Container container;
public Mythread41(Semaphore semaphore, Container container) {
this.semaphore = semaphore;
this.container = container;
}
@Override
public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " t1增加:" + i);
container.addNumber(i);
if (container.getSize() == 5) {
semaphore.release();
}
}
}
}
class Mythread42 implements Runnable {
private Semaphore semaphore;
private Container container;
public Mythread42(Semaphore semaphore, Container container) {
this.semaphore = semaphore;
this.container = container;
}
@Override
public void run() {
System.out.println("t2开始");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " t2结束========--");
semaphore.release();
}
}
5、使用ReentrantLock 实现
package com.example.demo.jucpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
/**
* 使用ReentrantLock 实现 --lock()/unlock()
*
* @author zl
*
*/
public class CorePoolDemo4 {
public static void main(String[] args) {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(100), threadFactory, new ThreadPoolExecutor.AbortPolicy());
Container container = new Container();
Lock lock = new ReentrantLock();
poolExecutor.execute(new Mythread52(lock, container));
poolExecutor.execute(new Mythread51(lock, container));
poolExecutor.shutdown();
}
}
class Mythread51 implements Runnable {
private Container container;
private Lock lock;
public Mythread51(Lock lock, Container container) {
this.lock = lock;
this.container = container;
}
@Override
public void run() {
lock.lock();
for (int i = 0; i < 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " t1增加:" + i);
container.addNumber(i);
if (container.getSize() == 5) {
lock.unlock();
}
}
}
}
class Mythread52 implements Runnable {
private Lock lock;
private Container container;
public Mythread52(Lock lock, Container container) {
this.lock = lock;
this.container = container;
}
@Override
public void run() {
System.out.println("t2开始");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
lock.lock();
System.out.println(Thread.currentThread().getName() + " t2结束========--");
lock.unlock();
}
}
6、countDownLatch 实现
package com.example.demo.jucpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
/**
* countDownLatch 实现 ---await/countDown
*
* @author zl
*
*/
public class CorePoolDemo5 {
public static void main(String[] args) {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(100), threadFactory, new ThreadPoolExecutor.AbortPolicy());
Container container = new Container();
CountDownLatch countDownLatch = new CountDownLatch(1);
poolExecutor.execute(new Mythread62(countDownLatch, container));
poolExecutor.execute(new Mythread61(countDownLatch, container));
poolExecutor.shutdown();
}
}
class Mythread61 implements Runnable {
private Container container;
private CountDownLatch countDownLatch;
public Mythread61(CountDownLatch countDownLatch, Container container) {
this.countDownLatch = countDownLatch;
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " t1增加:" + i);
container.addNumber(i);
if (container.getSize() == 5) {
countDownLatch.countDown();
}
}
}
}
class Mythread62 implements Runnable {
private CountDownLatch countDownLatch;
private Container container;
public Mythread62(CountDownLatch countDownLatch, Container container) {
this.countDownLatch = countDownLatch;
this.container = container;
}
@Override
public void run() {
System.out.println("t2开始");
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " t2结束========--");
}
}