Java多线程总结

  • 使用Thread创建线程;
  • 使用Runnable创建线程;
  • 实例:让人能够同时说话和开车;
  • 实例:模拟生成者和消费者。

1、使用Thread实现多线程

package excese;
public class ThreadTest extends Thread {

	public boolean b = false; 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ThreadTest thread = new ThreadTest();
		thread.setName("child");
		thread.start();
		try{
			thread.join();//等待该线程结束
		}catch(InterruptedException e ){
			e.printStackTrace();
		}
		ThreadTest thread1 = new ThreadTest();
		thread1.setName("child_222222");
		thread1.start();
		
		for(int i=0;i<5;i++){
			try{
				sleep(100);
			}catch(InterruptedException e ){
				e.printStackTrace();
			}
			System.out.println("---"+currentThread().getName()+":"+i); 
		}
		thread.b = true;
		thread1.b = true;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++){
			try{
				sleep(100);
			}catch(InterruptedException e ){
				e.printStackTrace();
			}
			System.out.println("++"+currentThread().getName()+"------"+i);
			if(b){
				break;
			}
		}
	}
	

}

2、使用Runnable实现多线程

public class RunnableTest implements Runnable{

	public static void main(String[] args) {
		Runnable r1 = new RunnableTest();
		Thread thread = new Thread(r1,"t1");
		thread.start();
		thread.setPriority(9);
		
		Thread thread2 = new Thread(r1,"t2");
		thread2.start();
		thread2.setPriority(2);
	}
	
	public void run(){
		for(int i=0;i<100;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}
}

3、实例:让人能够同时说话和开车

import static java.lang.System.out;

public class Person implements Runnable{

	int speakNo=0;
	int driveNo=0;
	
	private boolean canStop=false; // 是否停止线程
	
	public static void main(String[] args) {
		Person person = new Person();
		
		Thread t1 = new Thread(person,"speak"); // 第二个参数给出线程的名字
		Thread t2 = new Thread(person,"drive");
		
		t1.setPriority(8);
		t2.setPriority(4);
		
		t1.start();
		t2.start();
		
		try {
			Thread.currentThread().sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		person.setCanStop(true);
	}
	
	public void run(){
		while(true){
			String name = Thread.currentThread().getName(); // 得到当前线程的名字
			if(name.equals("speak")){
				speak();
			}else{
				drive();
			}
			if(canStop){
				break;
			}
		}
	}
	
	public void drive(){
		out.println("正在--------------开车!"+driveNo++);
//		try {
//			Thread.currentThread().sleep(5);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
	}
	
	public void speak(){
		out.println("正在说话!"+speakNo++);
//		try {
//			Thread.currentThread().sleep(5);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
	}

	public boolean isCanStop() {
		return canStop;
	}

	public void setCanStop(boolean canStop) {
		this.canStop = canStop;
	}
}

4、实例:模拟生成者消费者模型

import static java.lang.System.out;

// 第一步:实现Runnable接口,并且实现run方法
public class Factory implements Runnable {
	// 第二步:定义表示库存变量quantity,quantity的值会影响生产和消费这两个方法的执 
行,有产品才可以消费,仓库没有满才可以生产
	// 库存,初始为0,最大为10
	private int quantity = 0;
	// 第六步:创建成员变量canStop控制线程的结束,主线程中修改控制变量的值,在生产和 
消费线程中判断该变量的值,然后结束线程。
	
	private boolean canStop = false;

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public boolean isCanStop() {
		return canStop;
	}a

	public void setCanStop(boolean canStop) {
		this.canStop = canStop;
	}

	// 第四步:创建工厂对象,然后创建生产和消费的线程并启动
	public static void main(String args[]) {
		Factory factory = new Factory();

		Thread t1 = new Thread(factory, "producer1");
		Thread t2 = new Thread(factory, "producer2");
		Thread t3 = new Thread(factory, "consumer1");
		Thread t4 = new Thread(factory, "consumer2");

		t1.start();
		t2.start();
		t3.start();
		t4.start();

		try {
			Thread.currentThread().sleep(300);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		factory.setCanStop(true);
	}

	// 第五步:实现run方法,根据当前线程的任务去调用相应的方法,或者去生产 或者消费
	public void run() {
		String threadName = Thread.currentThread().getName();
		while (true) {
			if (threadName.startsWith("producer")) {
				produce();
			} else {
				consume();
			}
			if(canStop)
				break;
		}
	}

	// 第三步:定义消费和生产的方法
	public synchronized void consume() {
		// 判断是否有商品
		while (quantity == 0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		// 消费商品
		quantity--;
		String threadName = Thread.currentThread().getName();
		out.println(threadName + "消费了一个商品!");
		try {
			Thread.currentThread().sleep(50);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		notifyAll();
	}

	// 生成商品
	public synchronized void produce() {
		// 判断仓库是否已经满了,如果满了,就等待
		while (quantity == 10) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		// 生产
		quantity++;
		String threadName = Thread.currentThread().getName();
		out.println(threadName + "生产了一个商品!");
		try {
			Thread.currentThread().sleep(50);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		notifyAll();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙漏无语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值