class test {
    public synchronized void run() {
        //do something
    }
}

解决方案 »

  1.   

    补充一下:
    public class T{
       public synchronized void method(){
          //......
       }
    }
    相当于
    public class T{
       public void method(){
          synchronized(this){
             //......
          }
       }
    }
      

  2.   

    Object的实例有wait,notify等方法,但是只有the owner of the object's monitor 的线程才能调用,我问的是一个线程怎样才能成为 the owner of the object's monitor。上面是API文档里写的方法,我问的是第一种.
      

  3.   

    第一种方法指一个对象的synchronized方法被调用,调用者此时拥有此对象的锁:
    public class Test{
      private MyObject obj = new MyObject();
      class MyObject
      {
        public synchronized find()
        {........}
      }  class ThreadTest extends Thread
      { 
        public void run()
        {
          obj.find(); //这时,进入函数,这个线程拥有obj的锁
        }
      }
    }
      

  4.   

    1.这个过程不必你担心,是由java运行环境自动执行:当你在run()方法中使用了拥有synchronized说明的某个类的某个方法(如上面的
    class T的 method()方法),那这个线程就成为 the owner of method() 's monitor(),如果此时有另一个线程,调用这个方法,就必须等待.
    2.notify()只能在synchronized中出现,如:
    public class T{
       public synchronized void method(){
          //......
          notify()
       }
    }
            
    作用是释放等待的线程(即用wait()).
    3.在很多java教程中有一个"生产者/消费者"的教学程序很说明问题.
      

  5.   

    偶在CSDN常见到这类问题,比如偶参与过的地方
    http://www.csdn.net/expert/topic/1061/1061597.xml?temp=.3540613
    http://www.csdn.net/expert/topic/1059/1059474.xml?temp=.8966638
    偶告诉你,有很多地方了,搜索看看吧
      

  6.   

    public class ThreadTest
    {
    final static int maxOperations = 20;
    static int operatons = 0; WareHouse warehouse = new WareHouse();
    Producer pro = new Producer();
    Consumer con = new Consumer();
    public static void main(String[] args){ 
    ThreadTest test = new ThreadTest();
    test.beginTest();
    }
    public void beginTest()
    {
    pro.start();
    con.start();
    }
    class Producer extends Thread
    {
    int operations = 0;
    public void run()
    {
    System.out.println("Producer started");
    try
    {
    while(operations < maxOperations)
    warehouse.putIn(++operations);

    }
    catch (Exception e)
    {System.out.println(e); }
    }
    };
    class Consumer extends Thread
    {
    int operations = 0;
    public void run()
    {
    System.out.println("Consumer started");
    try
    {
    while(operations < maxOperations)
    warehouse.fetchOut(++operations);

    }
    catch (Exception e)
    {System.out.println(e); }
    }
    };class WareHouse
    {
    int wares;
    final int MAX_WARES = 5; public synchronized void putIn(int ops)
    {
    try
    {
    while(wares>=MAX_WARES){
    System.out.println("Producer:" + ops + "Warehouse full, I have to wait for consumer!");
    wait();//block me to let consumer have chance to fetchOut
    }
    //After consumer done, the consumer will notify me, Is can continue
    wares++;//put in one
    System.out.println("Producer:" + ops + "I put in one");
    //tell consumer that you can fetch because I have just putIn one
    notifyAll();
    System.out.println("Producer:" + ops + "I tell Consumer that he can fetch");

    }
    catch (Exception e)
    {
    System.out.println(e);
    }
    }
    public synchronized void fetchOut(int ops)
    {
    try
    {
    while(wares<=0){
    System.out.println("Consumer:" + ops + "Warehouse empty, I have to wait for producer!");
    wait();//block me to let producer have chance to putIn
    }
    //After the producer done, producer will notify me, I can continue
    wares--;//fetch one
    System.out.println("Consumer:" + ops + "I fetch one");
    //tell producer that you can putIn because I have just fetch one
    notifyAll();
    System.out.println("Consumer:" + ops + "I tell Producer that he can put in");
    }
    catch (Exception e)
    {
    System.out.println(e);
    }

    }
    };}