哪位大侠给详细讲下synchronized 用法?
以及与sleep关系?
还有就是线程调度的关系?
越详细越好。

解决方案 »

  1.   

    synchronized是在对象上加锁,当任务执行到synchronized关键字保护的代码的时候,就先查看锁是否可用,然后获取锁执行代码,释放锁。在多线程的程序中为了保证共享单元的线程安全而存在的,如果你不能使用原子性来替代无锁代码,那么就是用synchronized来保证线程同步。
    synchronized的用法:我能想到的就是这几点,所有对象都含有单一的锁,在该对象上调用任何synchronized方法,此对象都被加锁,这种情况下在调用此对象的其他方法是要阻塞的。二是在一个synchronized方法里调用其他的方法是不会释放锁的,等等
    在java se5里有了新的锁机制就是显示的调用lock对象来实现锁定和释放,但是它只在去要尝试加锁但有可能加锁失败的情况下才使用,使用它的TryLock方法,此方法还可以设置超时,除此之外建议使用synchronized,因为使用它的代码量更少,而且不容易出错。
    在java se5里有了新的sleep版本,如TimeUnit.MILLISECONDS.sleep(1000)。它可以让线程休眠一段时间,个人没觉得它与synchronized有什么特别的关系
    线程调度是cpu的事,你可以设置优先级,或者在优先级相同的线程间调用yield()方法来把cup让给其他线程,这些都是不严谨的做法,synchronized只是在保证线程同步。
      

  2.   

    去 google 上找找有简明的教程,看了就明白~
      

  3.   

    synchronized是同步,也就是只有一个线程获取该代码段的使用权,直到该线程跳出,然后其他线程才有机会执行。
    sleep就是要求该线程睡眠,停止执行,但并不放弃该对象的锁,睡眠时间后,线程就会进入就绪状态,和wait是有区别的。
      

  4.   

    public class TT implements Runnable{
    static int b = 100;
    public synchronized void m1() throws Exception{
    b = 1000;
    Thread.sleep(50000);
    System.out.println("m1----b = " + b);
    System.out.println("tt.b = "+ TT.b);
    }
    public synchronized void m2() throws Exception{
    Thread.sleep(5000);
    b = 2000;
    System.out.println("m2-----b =" + b);
    }
    public void run(){
    try{
    m1();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws Exception{
    TT tt = new TT();
    Thread t = new Thread(tt);
    t.start();
     tt.m2();
    Thread.sleep(5000);
    //System.out.println("b =" + tt.b);
    tt.b =  5000;
    System.out.println("b =" + tt.b);
    }
    }
    public class TT implements Runnable{
    static int b = 100;
    public synchronized void m1() throws Exception{
    b = 1000;
    Thread.sleep(50000);
    System.out.println("m1----b = " + b);
    System.out.println("tt.b = "+ TT.b);
    }
    public synchronized void m2() throws Exception{
    Thread.sleep(5000);
    b = 2000;
    System.out.println("m2-----b =" + b);
    }
    public void run(){
    try{
    m1();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws Exception{
    TT tt = new TT();
    Thread t = new Thread(tt);
    t.start();
     tt.m2();
    Thread.sleep(5000);
    //System.out.println("b =" + tt.b);
    tt.b =  5000;
    System.out.println("b =" + tt.b);
    }
    }public class TT implements Runnable{
    static int b = 100;
    public synchronized void m1() throws Exception{
    b = 1000;
    Thread.sleep(50000);
    System.out.println("m1----b = " + b);
    System.out.println("tt.b = "+ TT.b);
    }
    public synchronized void m2() throws Exception{
    Thread.sleep(5000);
    b = 2000;
    System.out.println("m2-----b =" + b);
    }
    public void run(){
    try{
    m1();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws Exception{
    TT tt = new TT();
    Thread t = new Thread(tt);
    t.start();
     tt.m2();
    Thread.sleep(5000);
    //System.out.println("b =" + tt.b);
    tt.b =  5000;
    System.out.println("b =" + tt.b);
    }
    }运行结果:
    m2-----b =2000
    b =5000
    m1----b=5000
    tt.b = 5000
      

  5.   

    哪位高手帮忙解释下 运行结果为什么是那样的?
    Thanks a lot!
      

  6.   

    从结果看执行顺序是这样的
    1:tt.m2(); 由于对象锁是单一锁,所以T线程中的m1方法此时是阻塞的, sleep是不会释放锁的
    2:M2执行完毕执行t线程中的run方法,即m1方法
    3:执行完run方法,换到主线程,sleep时间过后打印tt.b = 5000 
      

  7.   

    synchronized是同步的意思, 主要修饰方法和程序块, 当线程访问该方法时, 其他线程无法访问, 起到了线程安全作用, 而sleep是休眠的意思, 是个静态方法, 可用Thread.sleep()进行调用, 表示线程已经进入休眠状态, 但监控状态依然存在, 不会释放对象锁, 等到调用线程的唤醒方法, 就可以被其他线程访问, 至于线程调度是指按照特定机制为多个线程分配CPU的使用权!
      

  8.   


    synchronized 表示 这个方法 只能被一个 线程调用。。如果这个方法跳不出来。
     可能造成线程死锁。。就是 虽然有很多个线程,,但是只有一个线程在工作。。  sleep 就是让这个线程暂停多少毫秒。