线程有五种状态:创建、就绪、运行、阻塞、结束
isAlive:判断线程是否活着。就绪、运行、阻塞是活着,创建、结束是死了
getPriority、setPriority:设置优先级,明显的是,优先级高的,cpu分的时间片多一点
sleep:Thread.sleep(毫秒) 让当前线程休眠多少毫秒
join:合并两个线程,当执行到这个方法的时候,等当前线程执行完,再执行主线程。
yield:当某个线程正在cpu执行的时候,遇到这个方法,让出一次当前的cpu给其他的线程使用。
Object的wait:获得当前对象锁的线程阻塞;
Object的notify/notifyAll:唤醒在当前对象阻塞的其他线程;
一、死锁的java实现
package ss;
public class TestDeadLock implements Runnable{
public int flag = 1;
static Object o1 = new Object(),o2 = new Object();
public void run(){
System.out.println(“flag=”+flag);
if(flag==1){
synchronized(o1){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(o2){
System.out.println(“1”);
}
}
}
if(flag==0){
synchronized(o2){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(o1){
System.out.println(“0”);
}
}
}
}
public static void main(String[] args){
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}
二、生产者消费者的java实现
package ss;
public class ProducerConsumer {
public static void main(String[] args){
Basket basket = new Basket();
Producer p = new Producer(basket);
Producer p2 = new Producer(basket);
Producer p3 = new Producer(basket);
Consumer c = new Consumer(basket);
Consumer c2 = new Consumer(basket);
Consumer c3 = new Consumer(basket);
Thread tp = new Thread(p);
Thread tp2 = new Thread(p2);
Thread tp3 = new Thread(p3);
Thread tc = new Thread(c);
Thread tc2 = new Thread(c2);
Thread tc3 = new Thread(c3);
tp.setName(“生产者1号”);
tp2.setName(“生产者2号”);
tp3.setName(“生产者3号”);
tc.setName(“消费者1号”);
tc2.setName(“消费者2号”);
tc3.setName(“消费者3号”);
tp.start();
tp2.start();
tp3.start();
tc.start();
tc2.start();
tc3.start();
}
}
//生产的食物
class Food{
int id;
public Food(int id){
this.id = id;
}
public String toString(){
return id+””;
}
}
//装食物的篮子,以栈的形式呈现
class Basket{
int index = 0;//栈顶
Food[] foods = new Food[6];//篮子里最多装6个馒头
//生产食物
public synchronized void push(Food food){
//如果篮子里已经放满,阻塞当前线程
while(index == foods.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
//放入篮子里
foods[index] = food;
index ++;
}
//消费食物
public synchronized Food pop(){
//如果篮子里是空的,阻塞当前线程.后面的代码不再执行。失去对象锁。
while(index == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//唤醒其他等待该对象的线程(无法唤醒自己,只有别人能唤醒自己。因为当前对象已经被锁定。唤醒只是将其他阻塞的 线程到就绪状态,等当前线程执行完毕,都回到同一起跑线)
this.notifyAll();
//从篮子里拿出来吃
index — ;
Food food = foods[index];
return food;
}
}
//生产者生产食物过程
class Producer implements Runnable{
Basket basket = null;
public Producer(Basket basket){
this.basket = basket;
}
//每次生产20个食物
public void run(){
for(int i=1;i<=20;i++){
Food food = new Food(i);
basket.push(food);
System.out.println(Thread.currentThread().getName()+”生产食物:”+food);
//生产一个休息会儿
try {
Thread.sleep((int)Math.random()*100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费者消费食物过程
class Consumer implements Runnable{
Basket basket = null;
public Consumer(Basket basket){
this.basket = basket;
}
//消费20个食物
public void run(){
for(int i=1;i<=20;i++){
Food food = basket.pop();
System.out.println(Thread.currentThread().getName()+”消费了食物:”+food);
//消费完休息一会
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}