线程状态
-new 刚创建(new)
-Runnable就绪状态(start)
-running运行中(run)
-block 阻塞(sleep)
-terminated 结束
import java.util.Random;
public class TextRead {
public static void main(String[] args) throws Exception{
Storage storage = new Storage();
// TODO Auto-generated method stub
Thread consumer1 = new Thread(new Consumer(storage ));
consumer1.setName(“c1”);
Thread consumer2 = new Thread(new Consumer(storage));
consumer2.setName(“c2”);
Thread producer1 = new Thread(new Consumer(storage));
producer1.setName(“p1”);
Thread producer2 = new Thread(new Consumer(storage));
producer2.setName(“p2”);
}
}
class Storage{
//仓库容量为10
private Product[] products = new Product[10];
private int top = 0;
//生产者往仓库中放入产品
public synchronized void push(Product product) {
while (top == products.length ) {
try {
System.out.println(“producer wait”);
wait();//仓库已满,等待
}catch(InterruptedException e ) {
e.printStackTrace();
}
}
//把产品放入仓库
products[top++] = product;
System.out.println(Thread.currentThread().getName()+“生产产品”+product);
System.out.println(“producer notifyAll”);
notifyAll();//唤醒等待线程
}
//消费者从仓库中取出产品
public synchronized Product pop() {
while(top == 0) {
try {
System.out.println(“consumer wait “);
wait();//仓库空,等待
}catch(InterruptedException e) {
e.printStackTrace();
}
}
//从仓库中取出产品
–top;
Product p = new Product(products[top].getId(),products[top].getName());
products[top] = null;
System.out.println(Thread.currentThread().getName()+“消费了产品”+p);
System.out.println(“consumer notifyAll”);
notifyAll();
return p ;
}
}
class Consumer implements Runnable{
private Storage storage;
public Consumer(Storage storage) {
this.storage = storage;
}
public void run() {
int i = 0;
while(i<10) {
i++;
storage.pop();
try {
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer implements Runnable{
private Storage storage;
public Producer(Storage storage) {
this.storage = storage;
}
public void run() {
int i = 0;
Random r = new Random();
while(i<10) {
i++;
Product product = new Product(i,“电话”+r.nextInt());
storage.push(product);
}
}
}
class Product{
private int id;//产品id
private String name;//产品名称
public Product(int id,String name) {
this.id = id;
this.name = name;
}
public String toString() {
return”(产品ID”+id+“产品名称:”+name+")";
}
public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-线程被动的暂停和终止
依靠别的线程
没有及时释放资源
-线程主动暂停和终止
定期监测共享变量
如果需要暂停或者终止,先释放资源再主动动作
暂停:Thread.sleep(),休眠
终止:run方法结束,线程终止
-多线程死锁
每个线程互相持有别人需要的锁
预防死锁,对资源进行等级排序
-守护(后台)线程
普通线程的结束,是run方法运行结束
守护线程的结束,是run方法运行结束,或main函数
守护线程永远不要访问资源,如文件或数据库等
public class TextRead{
public static void main(String args[])throws InterruptedException{
Test t = new Test();
//标记 t 为守护线程
t.setDaemon(true);
t.start();
Thread.sleep(2000);
System.out.println(“main is running”);
}
}
//守护线程
class Test extends Thread{
public void run() {
while(true) {
System.out.println("thread is running ");
try {
Thread.sleep(1000);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
————————————————————————
thread is running
thread is running
thread is running
main is running