线程协作
生产者消费者模式
线程通信方法
解决一
管程法
package test1;
//测试生产者消费者模型--》利用缓冲区解决,管程法
public class TestPC {
public static void main(String[] args) {
SynContainer synContainer = new SynContainer();
Productor productor = new Productor(synContainer);
Consumer consumer = new Consumer(synContainer);
productor.start();
consumer.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++) {
container.push(new Product(i));
System.out.println("生产了" + 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 Product{
int id;
public Product(int id) {
this.id = id;
}
}
//缓冲区
class SynContainer{
private int count = 0;
Product[] products = new Product[10];//容器,产品类型的数组
public synchronized void push(Product product){//生产者放入产品需要使用synchronized方法
//如果容器满了则需要等待消费者消费
if(count == 10){
//通知生产者等待,消费者消费
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没满则告诉生产者生产,丢入产品
products[count] = product;
count++;
//通知消费者消费
this.notifyAll();
}
//消费者消费产品
public synchronized Product pop(){
//判断能否消费
if(count == 0){
//通知消费者等待,生产者生产
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没空则告诉消费者消费
count--;
Product product = products[count];
//通知生产者生产
this.notifyAll();
return product;
}
}
/*有一个生产者,还有一个消费者,生产者只管生产,消费者只管消费,产品是一个对象实例,被生产者生产,又被消费者消费,还需要一个缓冲区,如果缓冲区满了,就通知消费者消费,再通知生产者等待生产,如果没满,就可以将生产的产品放入缓冲区,然后可以通知消费者消费,
再有一个消费方法,如果缓冲区空了,就通知消费者停止消费,然后如果没满,就将缓冲区中的一个产品消费,然后再通知生产者生产产品*/
信号灯法
标志位解决
package test1;
//信号灯法,标志位解决
public class Demo3 {
public static void main(String[] args) {
TV tv = new TV();
new Actor(tv).start();
new Watcher(tv).start();
}
}
//生产者--》演员,表演
class Actor extends Thread{
TV tv;
public Actor(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2!= 0){
try {
this.tv.action("快乐大本营");
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
try {
this.tv.action("抖音!!!!!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//消费者————》观众,看表演
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{
//演员表演,观众等待,true
//观众观看,演员等待,false
String voice;//表演的节目
boolean flag = true;
//表演
public synchronized void action(String voice) throws InterruptedException {
if (!flag){
this.wait();
}
System.out.println("演员表演了"+ voice);
this.notifyAll();
this.voice = voice;
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 test1;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestPool {
public static void main(String[] args) {
//开启线程池,10个
ExecutorService service = Executors.newFixedThreadPool(10);
//提交线程实例,执行run方法
service.submit(new Acted());
service.submit(new Acted());
service.submit(new Acted());
service.submit(new Acted());
service.submit(new Acted());
service.shutdown();//关闭线程池
}
}
class Acted implements Runnable
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
package test1;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class TestA {
public static void main(String[] args) throws ExecutionException, InterruptedException {
B b = new B();
new Thread(b).start();
C c = new C();
c.start();
D d = new D();
FutureTask<Integer> task = new FutureTask<Integer>(d);
Thread thread = new Thread(task);
thread.start();
// thread.start();若放在task。get()后面则会出现线程阻塞
Integer integer = task.get();
System.out.println(integer);
}
}
class B implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "Runnable");
}
}
class C extends Thread{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "Thread");
}
}
class D implements Callable{
@Override
public Object call() throws Exception {
System.out.println(Thread.currentThread().getName() + "Callable");
return 100;
}
}
标签:Thread,tv,void,class,协作,线程,new,public
From: https://www.cnblogs.com/1234sdg/p/17031599.html