线程同步处理
java中提供synchronized关键字实现同步处理,同步的关键是要为代码加上锁,而对于所锁的操作方法有两种:同步方法和同步代码块
测试类
public class Test {
public static void main(String[] args) {
MyThread m = new MyThread();
new Thread(m,"售票员A").start();
new Thread(m,"售票员B").start();
new Thread(m,"售票员C").start();
}
}
使用同步方法
public class MyThread implements Runnable {
private int ticket = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
if(ticket>0) {
ticket--;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖了第"+(10-this.ticket)+"票");
}
else {
System.out.println("票卖光了");
break;
}
}
}
使用同步代码块
public class MyThread implements Runnable {
private int ticket = 100;
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (this) {
if (ticket > 0) {
ticket--;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖了第" + (100 - this.ticket) + "票");
} else {
System.out.println("票卖光了");
break;
}
}
}
}
}
生产者与消费者
生产者生产什么消费者就消费什么。并且生产者与消费者交替进行
商品
public class Product {
//品牌
private String name;
//名字
private String brand;
private Boolean flag = true;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public synchronized void setProduct(String brand,String name) {
if(flag==false) {
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setBrand(brand);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setName(name);
System.out.println(Thread.currentThread().getName() + ":" + "生产了" + this.getBrand().concat(this.getName()));
super.notify();
flag=false;
}
public synchronized void getProduct() {
if(flag==true)
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + "买了" + getBrand() + "" + getName());
super.notify();
flag=true;
}
}
消费者
public class Consumer implements Runnable {
private Product p;
public Consumer(Product p) {
this.p = p;
}
public void run() {
for (int i = 0; i < 10; i++) {
p.getProduct();
}
}
}
生产者
public class Producter implements Runnable {
private Product p;
public Producter(Product p) {
this.p = p;
}
public void run() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
p.setProduct("蒙牛", "核桃奶");
} else {
p.setProduct("康师傅", "方面面");
}
}
}
}
测试类
public class Test {
public static void main(String[] args) {
Product product = new Product();
Producter producter = new Producter(product);
Consumer consumer = new Consumer(product);
Thread t1 = new Thread(producter,"生产者");
Thread t2 = new Thread(consumer,"消费者");
t1.start();
t2.start();
}
运行效果