Java学习之多线程 5
线程安全:
一、
public class UnsafeBuyTicket {
public static void main(String[] args) {
BuyTicket person = new BuyTicket();
new Thread(person,"路人甲").start();
new Thread(person,"路人乙").start();
new Thread(person,"路人丙").start();
}
}
class BuyTicket implements Runnable{
private int ticketNums = 10;
boolean flag = true;
@Override
public void run() {
while (flag){
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void buy() throws InterruptedException {
if (ticketNums<=0){
flag = false;
return;
}
//线程延时,增加可能性
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+"买到了第"+ticketNums--+"张票。");
}
}
二、
import java.util.concurrent.locks.ReentrantLock;
public class TestLock {
public static void main(String[] args) {
SafeBuyTicket buyTicket = new SafeBuyTicket();
new Thread(buyTicket).start();
new Thread(buyTicket).start();
new Thread(buyTicket).start();
}
}
class SafeBuyTicket implements Runnable{
int ticketNums = 10;
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock(); //加锁
if (ticketNums>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNums--);
}else {
break;
}
}finally {
lock.unlock(); //解锁
}
}
}
}
三、
import java.util.concurrent.CopyOnWriteArrayList;
public class TestJUC {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
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.size());
}
}
线程通信——消费者与生产者问题
package PrimeThread.syn;
/**
* @author りChic丶安漾
* 生产者与消费者问题: 解决:管程法
*/
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
class Productor extends Thread{
SynContainer container;
public Productor(SynContainer container){
this.container = container;
}
//生产
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("生产了第"+i+"只鸡");
container.push(new Chicken(i));
}
}
}
class Consumer extends Thread {
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
//消费
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了"+container.pop().id+"只鸡");
}
}
}
class Chicken {
int id;
public Chicken(int id) {
this.id = id;
}
}
class SynContainer{
//需要的容器大小
Chicken[] chickens = new Chicken[10];
//容器计数器
int count = 0;
//生产者放入产品
public synchronized void push(Chicken chicken){
if (count==chickens.length){
//如果容器满了,消费者等待
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果容器未满,丢入产品
chickens[count] = chicken;
count++;
this.notifyAll();// 通知消费者消费
}
//消费者消费产品
public synchronized Chicken pop(){
if (count == 0){
//等待生产者生产
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//可以消费
count--;
Chicken chicken = chickens[count];
this.notifyAll();
return chicken;
}
}
二、信号灯法
package PrimeThread.syn;
/**
* @author りChic丶安漾
* 信号灯法
*/
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者:演员
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2==0){
this.tv.play("快乐大本营");
}else {
this.tv.play("抖音");
}
}
}
}
//消费者:观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
//产品:节目
class TV{
//演员表演,观众等待
//观众观看,演员等待
String voice;
boolean flag = true;
//表演
public synchronized void play(String voice){
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了:"+voice);
this.voice = voice;
//通知观众观看
this.notifyAll();
this.flag = !this.flag;
}
//观看
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观众观看了:"+voice);
this.notifyAll();
this.flag = !this.flag;
}
}
线程池
package PrimeThread.syn;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//测试线程池
public class TestPool {
public static void main(String[] args) {
/*
创建服务,创建线程池
newFixedThreadPool :参数为线程池的大小
*/
ExecutorService service = Executors.newFixedThreadPool(10);
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//关闭连接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}