Java 线程

  1. 继承 Thread 类,重写 run 方法
  2. 实现Runnable 接口

 

new Thread(new Runnable() {

    @Override

    public void run() {

    }

});

 

class t1 extends Thread{

    @Override

    public void run() {

        super.run();

    }

}

 

class t2 implements Runnable{

    @Override

    public void run() {

        

    }

}

Object上有一个

a.wait();

a.notify();

a.notifyAll();

用于线程同步

调用wait 时,线程进入阻塞,等待其他线程调用notifynotifyAll激活,

Thread中有 sleep ,使当前线程进入休眠,

yield(); 使线程出让

synchronized(a){

                

}

代码块的同步,配合wait notify 使用。Wait使当前线程交出锁,并进入wait池,notify 使进入已交出锁阻塞的线程进入另lock池,等待获得锁

Synchronized 是根据对象来锁定的。

 

线程同步还可以通过Lock 来同步

Readlock 读锁 WriteLock 写锁,相互配合使用

ReentrantLock 互斥锁

 

 

 

posted @ 2014-06-15 12:11  liushan  阅读(193)  评论(0编辑  收藏  举报