多线程2

博客主要介绍Java中的线程同步处理。Java通过synchronized关键字实现同步,关键是为代码加锁,操作方法有同步方法和同步代码块。还阐述了生产者与消费者模式,生产者生产的商品由消费者消费,二者交替进行,并给出测试类及运行效果。

线程同步处理

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();
	}

运行效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值