初学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”特性,应当是原子操作,也就是不能中断的。请问结果为什么不对?
* 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”特性,应当是原子操作,也就是不能中断的。请问结果为什么不对?
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;
}