初学Java,有一个线程同步的问题请教:/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threadtest;import java.util.logging.Level;
import java.util.logging.Logger;/**
 *
 * @author magi
 */
public class Main {    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Thread t1 = new Thread(new ThreadTest("Me"));
        Thread t2 = new Thread(new ThreadTest("NotMe"));
        t1.start();
        t2.start();
    }
}class ThreadTest implements Runnable {    public ThreadTest(String init) {
        caller = init;
    }    void runThread() {
        Object O = new Object();        synchronized (O) {
            for (int i = 0; i < 5; i++) {
                try {
                    System.out.println(caller + ": The size of Mimi is=" + mimi.toString());
                    Thread.sleep(10);
                    mimi++;
                    Thread.sleep(10);
                    System.out.println(caller + ": The size of mimi is=" + mimi.toString());
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }    public void run() {
        runThread();
    }
    public String caller;
    static public Integer mimi =
            10;
}这个程序输出是:
Me: The size of Mimi is=10
NotMe: The size of Mimi is=10
Me: The size of mimi is=12
Me: The size of Mimi is=12
NotMe: The size of mimi is=12
NotMe: The size of Mimi is=12
Me: The size of mimi is=14
Me: The size of Mimi is=14
NotMe: The size of mimi is=14
NotMe: The size of Mimi is=14
Me: The size of mimi is=16
Me: The size of Mimi is=16
NotMe: The size of mimi is=16
NotMe: The size of Mimi is=16
Me: The size of mimi is=17
Me: The size of Mimi is=17
NotMe: The size of mimi is=19
NotMe: The size of Mimi is=19
Me: The size of mimi is=19
NotMe: The size of mimi is=20而我看到书上讲的,加入synchronized修饰符,就相当于所谓的“atomic”特性,应当是原子操作,也就是不能中断的。请问结果为什么不对?

解决方案 »

  1.   

    Object   O   =   new   Object(); 因为上面这句!因为这样一来,每个线程都有自己的Object对象,各自锁各自的,也就谈不上同步了需要把O对象放出到线程外,确保该对象只能被生成一次,这样就能所有的线程都共享该对象了。BTW,在Java中,一般用一个byte[0]的数组来替代一个Object
      

  2.   

    楼上正确。
    synchronized 虽让强制拿到了锁,但可惜这个锁只属于这个线程而已
    package test.thread;import java.util.logging.Level;
    import java.util.logging.Logger;/**
     * @author magi
     */
    public class Test3 {
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
        // TODO code application logic here
        Thread t1 = new Thread(new ThreadTest("Me"));
        Thread t2 = new Thread(new ThreadTest("NotMe"));
        t1.start();
        t2.start();
      }
    }class ThreadTest implements Runnable {
      public ThreadTest(String init) {
        caller = init;
      }  static Object O = new Object(); // 把锁拿到这里看看吧  void runThread() {
        synchronized (O) {
          for (int i = 0; i < 5; i++) {
            try {
              System.out.println(caller + ":   The   size   of   Mimi   is=" + mimi.toString());
              Thread.sleep(10);
              mimi++;
              Thread.sleep(10);
              System.out.println(caller + ":   The   size   of   mimi   is=" + mimi.toString());
            } catch (Exception ex) {
              Logger.getLogger(ThreadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
        }
      }  public void run() {
        runThread();
      }  public String caller;  static public Integer mimi = 10;
    }