ITeye博客

java 基础

kiikoo · 2011-01-05 20:13 · 编程语言 · Java · thread · Markdown

一、sleep() yield() jion() 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁。也就是如果有Synchronized同步块,其他线程仍然不同访问共享数据。注意该方法要捕获异常。 比如有两个线程同时执行(没有Synchronized),一个线程优先级为MAX_PRIORITY,另一个为MIN_PRIORITY,如果没有Sleep()方法,只有高优先级的线程执行完成后,低优先级的线程才能执行;但当高优先级的线程sleep(5000)后,低优先级就有机会执行了。 总之,sleep()可以使低优先级的线程得到执行的机会,当然也可以让同优先级、高优先级的线程有执行的机会。 2.yield() 它与sleep()类似,只是不能由用户指定暂停多长时间,并且yield()方法只能让同优先级的线程有执行的机会。 3.join() join()方法使调用该方法的线程在此之前执行完毕,也就是等待调用该方法的线程执行完毕后再往下继续执行。注意该方法也要捕获异常。 下面代码a的值不一定为5,看机器情况。


public class TestJava {

    private static int a = 0;

    public static void main(String[] args) {
        Test t = new TestJava().new Test();
        Thread th = new Thread(t);
        th.start();
        System.out.println(a);
    }

    public class Test implements Runnable {

        public void run() {
            for (int i = 0; i < 5; i++) {
                a += 1;
            }
        }

    }
}

使用jion方法之后,可以保证每次执行a都为5


public class TestJava {

    private static int a = 0;

    public static void main(String[] args) throws InterruptedException {
        Test t = new TestJava().new Test();
        Thread th = new Thread(t);
        th.start();
        th.join();
        System.out.println(a);
    }

    public class Test implements Runnable {

        public void run() {
            for (int i = 0; i < 5; i++) {
                a += 1;
            }
        }

    }
}

二、wait() notify() notifyAll() 这3个方法属于Object基础类,也就是说每个对像都有wait(),notify(),notifyAll() 的功能.因为都个对像都有锁,锁是每个对像的基础。

这三个方法用于协调多个线程对共享数据的存取,所以必须在Synchronized语句块内使用这三个方法。前面说过Synchronized这个关键字用于保护共享数据,阻止其他线程对共享数据的存取。但是这样程序的流程就很不灵活了,如何才能在当前线程还没退出Synchronized数据块时让其他线程也有机会访问共享数据呢?此时就用这三个方法来灵活控制。 wait()方法使当前线程暂停执行并释放对象锁标志,让其他线程可以进入Synchronized数据块,当前线程被放入对象等待池中。当调用 notify()方法后,将从对象的等待池中移走一个任意的线程并放到锁标志等待池中,只有锁标志等待池中的线程能够获取锁标志;如果锁标志等待池中没有线程,则notify()不起作用。notifyAll()则从对象等待池中移走所有等待那个对象的线程并放到锁标志等待池中。


package  ProductAndConsume;

import  java.util.List;

public   class  Consume  implements  Runnable{
     private  List container  =   null ;
     private   int  count;
     public  Consume(List lst){
      this .container  =  lst;
    }
  public   void  run() {
  
   while ( true ){
    synchronized  (container) {
     if (container.size() ==   0 ){
      try  {
      container.wait(); // 放弃锁 
     }  catch  (InterruptedException e) {
      e.printStackTrace();
     }
    }
     try  {
     Thread.sleep( 100 );
    }  catch  (InterruptedException e) {
      //  TODO Auto-generated catch block 
     e.printStackTrace();
    }
    container.remove( 0 );
    container.notify();
    System.out.println( " 我吃了 " + ( ++ count) + " 个 " );
   }
  }
  
 }

}

package  ProductAndConsume;

import  java.util.List;

public   class  Product  implements  Runnable {
  private  List container  =   null ;
     private   int  count;
  public  Product(List lst) {
   this .container  =  lst;
 }

  public   void  run() {
   while  ( true ) {
    synchronized  (container) {
     if  (container.size()  >  MultiThread.MAX) {
      try  {
      container.wait();
     }  catch  (InterruptedException e) {
      e.printStackTrace();
     }
    }
     try  {
     Thread.sleep( 100 );
    }  catch  (InterruptedException e) {
     e.printStackTrace();
    }
    container.add( new  Object());
    container.notify();
    System.out.println( " 我生产了 " + ( ++ count) + " 个 " );
   }
  }

 }

}

package  ProductAndConsume;

import  java.util.ArrayList;
import  java.util.List;

public   class  MultiThread {
  private  List container  =   new  ArrayList();


 public final static int MAX = 5;

 

  public static void main(String args[]){

 MultiThread m  =   new  MultiThread(); 

   new  Thread( new  Consume(m.getContainer())).start();
   new  Thread( new  Product(m.getContainer())).start();
   new  Thread( new  Consume(m.getContainer())).start();
   new  Thread( new  Product(m.getContainer())).start();
 }
  public  List getContainer() {
   return  container;
 }

  public   void  setContainer(List container) {
   this .container  =  container;
 } 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。