public class SellTicket implements Runnable{
private int ticket =100;
private Object obj = new Object();
@Override
public void run() {
while(true){
synchronized (obj){ //加入锁,将同步代码块放入锁内
if (ticket >0){//设置休眠时间,每一个线程执行一次后会休眠100ms
try{Thread.sleep(100);}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+"正在出售第"+ticket+"张票");
ticket--;
}
}
}
}
}
线程安全的类:
这三个类里面的方法都加了synchronized关键字
stringBuffer: 线程安全的、可变的字符序列;用法与StringBuild一致,
Vector:实现了list接口,用法与Arraylist一致,且是线程安全的,如果不需要线程安全的实现,建议使用Arraylist
HashTable:是map<> 的一员,与hashmap用法一致,且是线程安全的,如果不需要线程安全的实现,建议使用hashmap;
也可通过:
List<String> list = Collections.synchronizedlist(new ArrayList<String>());
转化;
生产者和消费者的方法:
public class Box {
//表示第几瓶奶
private int milk;
//定义一个成员变量,表示奶箱的状态;
private boolean state = false;
//存牛奶和获取牛奶的方法
//wait和notify方法必须具有锁对象才能使用,即必须有锁对象;
public synchronized void put(int milk) {
//如果有牛奶,等待消费
if (state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 没有牛奶,就生产牛奶
this.milk = milk;
System.out.printf("送第%d瓶奶到奶箱",this.milk,"\n");
state = true;
notifyAll(); //执行生产后需要唤醒其他线程
}
public synchronized void get(){
//如果没有牛奶,等待生产
if(!state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//有牛奶就消费
System.out.printf("取第%d瓶奶",this.milk,"\r\n");
state = false;
notifyAll();
}
}
public class Producer implements Runnable {
private Box b;
public Producer(Box b) {
this.b = b;
}
@Override
public void run() {
for (int i =1;i<=5;i++){
b.put(i);
}
}
}
public class Customer implements Runnable{
private Box b;
public Customer(Box b) {
this.b=b;
}
@Override
public void run() {
while(true){
b.get();
}
}
}
public class BoxDemo {
public static void main(String[] args) {
Box b = new Box();
Producer p = new Producer(b);
Customer c = new Customer(b);
//创建两个线程对象,分别把生产者对象和消费者对象作为构造方法的参数传入;
Thread t1 =new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}