多线程之死锁和生产者消费者问题
死锁
package com.java.thread;
public class DeadLock {
public static void main(String[] args) {
Transform boy1=new Transform(0,"boy1");
Transform boy2=new Transform(1,"boy2");
boy1.start();
boy2.start();
}
}
class Car{
}
class Train{
}
class Transform extends Thread{
static Car car=new Car();
static Train train = new Train();
int choice;
String name;
Transform(int choice,String name){
this.choice=choice;
this.name=name;
}
@Override
public void run() {
try {
transform();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void transform() throws InterruptedException {
if(choice==0){
synchronized (car){
System.out.println(this.name+"获得了玩具汽车");
Thread.sleep(1000);
synchronized (train){
System.out.println(this.name+"获得了玩具火车");
}
}
}else{
synchronized (train){
System.out.println(this.name+"获得了玩具火车");
Thread.sleep(2000);
synchronized (car){
System.out.println(this.name+"获得了玩具汽车");
}
}
}
}
}
Lock锁
package com.java.thread;
import java.util.concurrent.locks.ReentrantLock;
public class TestLock {
public static void main(String[] args) {
Ticket ticket=new Ticket();
new Thread(ticket,"John").start();
new Thread(ticket,"Jams").start();
new Thread(ticket,"Love").start();
}
}
class Ticket implements Runnable{
int ticketNums=10;
private final ReentrantLock lock =new ReentrantLock();
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
lock.lock();
if(ticketNums>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"票");
}else{
break;
}
}finally {
lock.unlock();
}
}
}
}
生产者-消费者问题
package com.java.thread;
public class TestPC {
public static void main(String[] args) {
SynContainer container=new SynContainer();
new Product(container).start();
new Consumer(container).start();
}
}
class Product extends Thread{
SynContainer container;
public Product(SynContainer container){
this.container=container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
container.push(new Things(i));
System.out.println("生产了"+i+"个");
}
}
}
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container){
this.container=container;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 100; i++) {
System.out.println("消费了"+container.pop().id+"个");
}
}
}
class Things {
int id;
Things(int id){
this.id=id;
}
}
class SynContainer{
Things[] things=new Things[10];
int count=0;
public synchronized void push(Things thing){
if (count==things.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
things[count]=thing;
count++;
this.notify();
}
public synchronized Things pop(){
if(count==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Things thing=things[count];
this.notify();
return thing;
}
}
由生产者-消费者问题引申出的解决办法二——>信号灯法
package com.java.thread;
public class TestPC2 {
public static void main(String[] args) {
OutPut outPut=new OutPut();
new Odd(outPut).start();
new Even(outPut).start();
}
}
class Odd extends Thread{
OutPut outPut;
public Odd(OutPut outPut){
this.outPut=outPut;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
if(i%2!=0){
outPut.OutOdd(i);
}
}
}
}
class Even extends Thread{
OutPut outPut;
public Even(OutPut outPut){
this.outPut=outPut;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
if(i%2==0){
outPut.OutEven(i);
}
}
}
}
class OutPut{
int number;
Boolean flag=true;
public synchronized void OutOdd(int number){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("打印奇数-->"+number);
this.notify();
this.flag=!this.flag;
}
public synchronized void OutEven(int number){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("打印偶数-->"+number);
this.notify();
this.flag=!this.flag;
}
}
多线程扩展-线程池
package com.java.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestPool {
public static void main(String[] args) {
ExecutorService service= Executors.newFixedThreadPool(10);
service.execute(new MyPool());
service.execute(new MyPool());
service.execute(new MyPool());
service.execute(new MyPool());
service.shutdown();
}
}
class MyPool implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}