Java–多线程–JUC、死锁、lock、生产者与消费者问题、线程池
JUC
package com.zy.thread;
import java.util.concurrent.CopyOnWriteArrayList;
public class StudyThreadJUC {
public static void main(String[] args) {
CopyOnWriteArrayList list = new CopyOnWriteArrayList();
for (int i = 0; i < 10000; i++) {
new Thread(()->{
list.add(Thread.currentThread().getName());
}).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("List大小:"+list.size());
}
}
死锁
package com.zy.thread;
public class StudyThreadDeadLock {
public static void main(String[] args) {
Makeup g1 = new Makeup(0, "灰姑娘");
Makeup g2 = new Makeup(1, "白雪公主");
g1.start();
g2.start();
}
}
class Lipstick{
}
class Mirror{
}
class Makeup extends Thread{
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;
String girlName;
public Makeup(int choice,String girlName){
this.choice = choice;
this.girlName = girlName;
}
private void makeup() throws InterruptedException {
if(choice == 0){
synchronized (lipstick){
System.out.println(this.girlName+"获得口红");
Thread.sleep(1000);
synchronized (mirror){
System.out.println(this.girlName+"获得镜子");
}
}
}else{
synchronized (mirror){
System.out.println(this.girlName+"获得镜子");
Thread.sleep(2000);
synchronized (lipstick){
System.out.println(this.girlName+"获得口红");
}
}
}
}
@Override
public void run() {
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
lock
package com.zy.thread.lock;
import java.util.concurrent.locks.ReentrantLock;
public class StudyThreadLock {
public static void main(String[] args) {
TestLock testLock = new TestLock();
new Thread(testLock).start();
new Thread(testLock).start();
new Thread(testLock).start();
}
}
class TestLock implements Runnable{
int ticketNums = 10;
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true){
lock.lock();
try {
if(ticketNums>0){
Thread.sleep(1000);
System.out.println(ticketNums--);
}else{
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
PCP–》管程法
package com.zy.thread.pcp;
public class StudyThreadPCP {
public static void main(String[] args) {
Buffer buffer = new Buffer();
new Productor(buffer).start();
new Consumer(buffer).start();
}
}
class Productor extends Thread{
Buffer buffer;
public Productor(Buffer buffer){
this.buffer = buffer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
buffer.push(new Product(i));
System.out.println("生产了"+i+"个产品");
}
}
}
class Consumer extends Thread{
Buffer buffer;
public Consumer(Buffer buffer){
this.buffer = buffer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了-->"+buffer.pop().id+"个产品");
}
}
}
class Product{
int id;
public Product(int id){
this.id = id;
}
}
class Buffer{
Product[] products = new Product[10];
int count;
public synchronized void push(Product product) {
if(count == products.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
products[count] = product;
count++;
this.notify();
}
public synchronized Product pop() {
if(count == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Product product = products[count];
this.notify();
return product;
}
}
PCP–》信号灯法
package com.zy.thread.pcp;
public class StudyThreadPCP2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Audience(tv).start();
}
}
class Player extends Thread{
private TV tv;
public Player(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
tv.play("第"+i+"个节目");
}
}
}
class Audience extends Thread{
private TV tv;
public Audience(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
tv.watch();
}
}
}
class TV{
private String program;
private boolean flag = true;
public synchronized void play(String program){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了:"+program);
this.program = program;
this.flag = !this.flag;
this.notify();
}
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观众观看了:"+program);
this.flag = !this.flag;
this.notify();
}
}
线程池
package com.zy.thread;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
public class StudyThreadPool {
public static void main(String[] args) {
ExecutorService service = new ThreadPoolExecutor(3,10,3,
TimeUnit.SECONDS,new ArrayBlockingQueue<>(4));
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"--》"+i);
}
}
}