纸绘武身(多线程)

package sunday;

import java.util.*;

/*
实现多线程
卖票
*/
public class

Demo

{
public static void main(String[] args) {
//thread();
//cellTicket();
boxDemo();
}//main
//thread方法
public synchronized void

thread()

throws InterruptedException {
Tools tools = new Tools();

Thread t1 = new Thread(tools, “窗口1”);
Thread t2 = new Thread(tools, “窗口2”);

//设置线程名称, 返回正在执行的线程对象, 设置优先级,
t1.setName(“name”);
Thread.currentThread();
t1.setPriority(10);
//停留, 等待线程死亡,守护线程
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

t1.setDaemon(true);
//线程安全与不安全的类,不安全类的转化
StringBuffer stringBuffer = new StringBuffer();
StringBuilder stringBuilder = new StringBuilder();

Vector strings = new Vector<>();
ArrayList strings1 = new ArrayList<>();

Hashtable<String, String> stringStringHashtable = new Hashtable<>();
HashMap<String, String> stringStringHashMap = new HashMap<>();

List list = Collections.synchronizedList(new ArrayList());
//使线程等待直到被唤醒,唤醒单个线程,唤醒所有线程
wait();
notify();
notifyAll();
}
public static void

idea()

{
/*
10优先级最高
存在数据安全问题标准:
多线程环境,共享数据,多条语句操作共享数据
同步方法锁的对象:this
同步静态方法锁的对象:类名.class
*/
}
//卖票
public static void

cellTicket()

{
Tools tools2 = new Tools();

Thread t1 = new Thread(tools2, “线程1”);
Thread t2 = new Thread(tools2, “线程2”);
Thread t3 = new Thread(tools2, “线程3”);

t1.start();
t2.start();
t3.start();
}
public static void

boxDemo()

{
Box box = new Box();
Producer producer = new Producer(box);
Customer customer = new Customer(box);

Thread thread = new Thread(producer);
Thread thread1 = new Thread(customer);

thread.start();
thread1.start();
}
}//end
package sunday;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class

Tools

implements Runnable{
private int tickets=1;
private Lock lock=new ReentrantLock();

@Override
public void run() {
while (true) {
try {
lock.lock();
if (tickets <=100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + “抢到了第” + tickets + “张票”);
tickets++;
}
}finally {
lock.unlock();
}
}
}
}//end
package sunday;

public class

Box

{
private int milk;
private Boolean aBoolean=false;

public synchronized void

put(int milk)

{
if(aBoolean){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.milk=milk;
System.out.println(“第”+this.milk+“个人正在接种疫苗”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
aBoolean=true;
notifyAll();
}

public synchronized void

get()

{
if(!aBoolean){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(“第”+this.milk+“个人已接种完成”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
aBoolean=false;
notifyAll();
}
}//end
package sunday;

public class

Producer

implements Runnable{
private Box box;
public Producer(Box box) {
this.box=box;
}

@Override
public void run() {
for(int i=1;i<=5;i++){
box.put(i);
}
}
}
package sunday;

public class

Customer

implements Runnable{
private Box box;
public Customer(Box box) {
this.box=box;
}

@Override
public void run() {
while (true){
box.get();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值