synchronized 的作用不是在线程访问该类的对象是把对象锁定等该语句执行完了以后才解锁的吗 为什么这样写 输出结果还是同时执行的呢 菜鸟学线程 求高手解答public class TestSynchronized {

public static void main(String[] args) {
T t1 = new T("t1");
T t2 = new T("t2");
t1.start();
t2.start();
}
public void run2() {

}
}
class T extends Thread { private static int num = 0; T(String n) { 
super(n);
}
public void run() {
synchronized(this) {
num++;
try {
Thread.sleep(1000);
} catch(InterruptedException e) {}
System.out.println(getName() + "------" + num);
}
}
}

解决方案 »

  1.   

    synchronized(this)锁定的是当前对象
      

  2.   

    给了一下,可能是你要的效果
    public class TestSynchronized {
        
        public static void main(String[] args) {
         TestSynchronized o = new TestSynchronized();
            T t1 = new T("t1",o);
            T t2 = new T("t2",o);
            t1.start();
            t2.start();
        }
        public void run2() {
            
        }
    }
    class T extends Thread {    private static int num = 0;
        TestSynchronized o;    T(String n,TestSynchronized target) { 
            super(n);
            o=target;
        }
        public void run() {
            synchronized(o) {
                num++;
                try {
                    Thread.sleep(1000);
                } catch(InterruptedException e) {}
                System.out.println(getName() + "------" + num);
            }
        }
    }
      

  3.   

    你锁住自身有什么用啊....你创建了两个对象,你产生同步的问题是num类变量,不是成员变量,锁住this没用
      

  4.   

    你可以试试synchronized(this.getClass())
      

  5.   

    锁住的是当前类的对象 但是我这么写也不行啊 t1 t2 同时调用的是ts类的对象 当t1 开始时候 调用ts类 ts应该被锁定的 怎么还不行呢 public class TestSynchronized { public static void main(String[] args) {
    T t1 = new T("t1");
    T t2 = new T("t2");
    t1.start();
    t2.start();
    }

    }
    class T extends Thread {
    TT ts = new TT();
    T(String n) { 
    super(n);
    }
    public void run() {
    ts.run2();
    }
    }
    class TT {
    private static int num = 0;
    public void run2() {
    synchronized(this) {
    num++;
    try {
    Thread.sleep(1000);
    } catch(InterruptedException e) {}
    System.out.println("------" + num);
    }
    }
    }
      

  6.   

    汗 你怎么还是锁this  麻烦你锁住变量num可以吗
      

  7.   

    我想搞明白 这里this 是不是就是指的ts吗? 我把ts都锁住了 ts里面的num变量不也就锁了吗 呵呵 我是菜鸟学线程~~~~
      

  8.   

     synchronized(this) ,lz这样写不对哦
      

  9.   

    this指的不是ts,要用this.getClass()。改成  synchronized(this.getClass())   即可。
      

  10.   

    public class TestSync implements Runnable {
      Timer timer = new Timer();
      public static void main(String[] args) {
        TestSync test = new TestSync();
        Thread t1 = new Thread(test);
        Thread t2 = new Thread(test);
        t1.setName("t1"); 
        t2.setName("t2");
        t1.start(); 
        t2.start();
      }
      public void run(){
        timer.add(Thread.currentThread().getName());
      }
    }class Timer{
      private static int num = 0;
      public synchronized void add(String name){ 
       //synchronized (this) {
        num ++;
        try {Thread.sleep(1);} 
        catch (InterruptedException e) {}
        System.out.println(name+", 你是第"+num+"个使用timer的线程");
      //}
      }
    }
     为什么这样用就可以
      

  11.   


    你这样可以,是因为你创建的两个线程访问的是同一个对象的synchronized的方法, 所以可以。之前不可以,是因为你创建的两个线程访问了两个不同的对象, 每一个线程都访问自己的对象里面的方法,所以锁不住。 如果你想一开始的那个例子也有同步效果的话, 你应该把那个同步块提出来, 把它变成一个静态的同步方法, 就能锁住了。楼主明白??
      

  12.   

    LZ要明白一个事情,同步是多个线程共同访问一个相同的资源才有意义的
    你的第一次写法,syncrhonized(this)就是每个线程自己锁自己(和别的线程没有任何干涉),所以达不到同步的效果public class TestSync implements Runnable {
      Timer timer = new Timer();
      public static void main(String[] args) {
        TestSync test = new TestSync();
        Thread t1 = new Thread(test);
        Thread t2 = new Thread(test);
        t1.setName("t1"); 
        t2.setName("t2");
        t1.start(); 
        t2.start();
      }
      public void run(){
        syncronized(this) { //没有这里也不可以
            timer.add(Thread.currentThread().getName());
        }
      }
    }
    这个例子可以(上面修改后),是因为t1和t2共同使用一个test楼主好好理解一下,第一次为什么没有共同使用一个对象?为什么上面这段程序是共同使用一个对象?
      

  13.   

    两个房间两把钥匙(this)当然两个房间都能打开咯,也就没有锁住,如果你两个房间只有一把钥匙(静态变量)就能锁住了另一个房间