zoukankan      html  css  js  c++  java
  • 线程随笔记

    线程创建方式

      1、继承Thread类,主线程中 new 实现类().start();线程名可以在子类构造方法中添加;

    1 class Demo extends Thread {
    2    Demo(String name){
    3       super(name);    
    4    }  
    5   public void run(){
    6       ...  
    7   }
    8 }

           2、实现Runnable接口,主线程中  new Thread (实现类对象,线程名).start()。

    线程的状态

      新生状态   线程对象创建好了,未调用 start 方法之前。就是 new Thread() 

      就绪状态           线程在调用 start 方法之后,进入就绪状态(可运行状态)

      运行状态      线程获得线程调度器的调度之后进入运行状态,CPU执行

      阻塞状态      线程运行是调用了  join/sleep/wait  方法,进入阻塞状态

      死亡状态         线程执行结束

    线程同步

      synchronized代码块、synchronized方法、lock

      同步代码块中可以使用任意锁。

      同步方法中不指定锁,当不是静态方法时,锁是 this  ; 当是静态方法时 ,锁是 类的字节文件  class ,可以用 getClass() 获取,也可以用当前 类名.class() 

      

      lock 接口 :将同步的隐式锁操作变成显式锁操作;condition 接口 :将 wait , notify , notifyAll 方法进行封装,变成监视器对象,可以任意锁组合

      Lock lock = new ReentrantLock();

      Condition con = lock.newCondition();

      lock.lock()   获取锁

      lock.unlock()  释放锁,通常定义在 finally 代码块中

      con.await()   等待,相当于wait();

      con.signal()    相当于 notify

      con.signalAll()   相当于 notifyAll.

     

    wait 与 sleep

      wait 进入等待,可以指定时间也可以不指定时间,会释放锁

      sleep 进入睡眠,必须指定时间,不会释放锁

    生产者与消费者

      1 import java.util.concurrent.locks.Condition;
      2 import java.util.concurrent.locks.Lock;
      3 import java.util.concurrent.locks.ReentrantLock;
      4 
      5 public class ProducterConsumer {
      6 
      7     public static void main(String[] args) {
      8         Resource r = new Resource();
      9         Produter p1 = new Produter(r);
     10         Produter p2 = new Produter(r);
     11         Cousumer c1 = new Cousumer(r);
     12         Cousumer c2 = new Cousumer(r);
     13         Thread t1 = new Thread(p1);
     14         Thread t2 = new Thread(p2);
     15         Thread t3 = new Thread(c1);
     16         Thread t4 = new Thread(c2);
     17         t1.start();
     18         t2.start();
     19         t3.start();
     20         t4.start();
     21     }
     22 
     23 }
     24 
     25 class Resource {
     26     private String name;
     27     private int count = 0;
     28     private boolean flag = false;
     29     Lock lock = new ReentrantLock();
     30     
     31     Condition prod_con = lock.newCondition();
     32     Condition cons_con = lock.newCondition();
     33     
     34     
     35     public void production(String name) {
     36         lock.lock();
     37         try {
     38             while(flag) {
     39                 prod_con.await();    //进入等待,释放锁
     40             }
     41             this.name = name + (++count);
     42             System.out.println(Thread.currentThread().getName() + "..生产了.." + this.name);
     43             flag = true;
     44             cons_con.signal();      //生产完毕,唤醒其他线程
     45         } catch (InterruptedException e) {
     46             e.printStackTrace();
     47         } finally {
     48             lock.unlock();
     49         }
     50     }
     51     
     52     public void consumption() {
     53         lock.lock();
     54         try {
     55             while(!flag) {
     56                 cons_con.await();    //进入等待,释放锁
     57             }
     58             System.out.println(Thread.currentThread().getName() + ".....消费了....." + this.name);
     59             flag = false;
     60             prod_con.signal();      //唤醒其他线程
     61         } catch (InterruptedException e) {
     62             e.printStackTrace();
     63         } finally {
     64             lock.unlock();
     65         }
     66     }
     67     
     68 }
     69 
     70 class Produter implements Runnable{
     71 
     72     private Resource r;
     73     
     74     Produter(Resource r){
     75         this.r = r;
     76     }
     77     
     78     @Override
     79     public void run() {
     80         while(true)
     81             r.production("粉蒸肉");
     82     }
     83     
     84 }
     85 
     86 class Cousumer implements Runnable{
     87 
     88     private Resource r;
     89     
     90     Cousumer(Resource r){
     91         this.r = r;
     92     }
     93     
     94     @Override
     95     public void run() {
     96         while(true)
     97             r.consumption();
     98     }
     99     
    100 }

    如有错误的地方,请指出。谢谢评论。

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood(度序列构图)
    poj1236
    poj1236
    有向图求强连通分量
    有向图求强连通分量
    zoj 2532(Internship )找割边
    zoj 2532(Internship )找割边
    Expedition POJ
    2016年第七届蓝桥杯C/C++ B组国赛 —— 第四题:机器人塔
    2016年第七届蓝桥杯C/C++ B组国赛 —— 第四题:机器人塔
  • 原文地址:https://www.cnblogs.com/mmling/p/8484032.html
Copyright © 2011-2022 走看看