JAVA之多线程

一、基本概念

    线程:一个程序里的不同路径        进程:一个exe文件,一个静态的概念

二、创建

(1)继承Thread

(2)实现Runnable接口

都需要重写run()方法,调用start()方法。最好使用第二种

三、状态转换

    创建-->start-->调度-->运行-->结束

    运行过程中可能会出现阻塞,之后会重新接受CPU的调度

四、常用方法

sleep:阻塞      interrupt:叫醒线程,最好不用,可以使用开关

join:合并线程,异步变同步,有先后顺序,你干完我再干

yield:让出CPU,让其他线程先执行

wait:让线程等待     notify:叫醒线程    notifyAll:叫醒全部等待线程

五、线程的优先级

优先级越高,获得的CPU时间片越多

六、线程同步

访问同一资源的多个线程之间的协调叫线程同步

解决办法:给当前对象加锁

synchronized = synchronized(对象){}    注意:锁的是对象,不是方法

七、死锁

举例:有2个线程1和2,还有2个对象A和B,线程1若要成功结束,需要先执行锁定的A,再执行锁定的B,线程2若要成功结束,需要先执行锁定的B,再执行锁定的A。若线程1先锁到A,线程2执行,锁定B,结果二者都结束不下去,造成死锁。

有一个经典案例就是哲学家吃饭问题。

注意:一个线程访问一个锁上的对象的一个方法过程中,另一个线程不用等待上一个线程结束到解锁,
可以访问锁上的对象的另一个方法

八、生产者消费者问题

package com.多线程;

public class ProducerConsumer {
	public static void main(String[] args) {
		SyncStack ss = new SyncStack();
		Producer p = new Producer(ss);
		Consumer c = new Consumer(ss);
		new Thread(p).start();
		new Thread(c).start();
	}
}

class WoTou {
	int id; 
	WoTou(int id) {
		this.id = id;
	}
	public String toString() {
		return "WoTou : " + id;
	}
}

class SyncStack {
	int index = 0;
	WoTou[] arrWT = new WoTou[6];
	
	public synchronized void push(WoTou wt) {
		while(index == arrWT.length) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notifyAll();		
		arrWT[index] = wt;
		index ++;
	}
	
	public synchronized WoTou pop() {
		while(index == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify();
		index--;
		return arrWT[index];
	}
}

class Producer implements Runnable {
	SyncStack ss = null;
	Producer(SyncStack ss) {
		this.ss = ss;
	}
	
	public void run() {
		for(int i=0; i<20; i++) {
			WoTou wt = new WoTou(i);
			ss.push(wt);
System.out.println("生产了:" + wt);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}			
		}
	}
}

class Consumer implements Runnable {
	SyncStack ss = null;
	Consumer(SyncStack ss) {
		this.ss = ss;
	}
	
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}		
		for(int i=0; i<20; i++) {
			WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}			
		}
	}
}

我在消费者的run方法第一行就加了sleep,是为了每次都让生产者生产,以免篮子里没东西,消费者就开始消费了。

九、wait和sleep的区别

(1)wait是Object的方法,sleep是Thread的方法

(2)wait时,别人可以访问锁对象,sleep时,别人不可以访问锁对象



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值