Java多线程问题

MagiSu 2008-01-21 01:31:41
初学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”特性,应当是原子操作,也就是不能中断的。请问结果为什么不对?
...全文
95 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
老紫竹 2008-01-21
  • 打赏
  • 举报
回复
楼上正确。
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;
}
MagiSu 2008-01-21
  • 打赏
  • 举报
回复
谢谢!
vlinux 2008-01-21
  • 打赏
  • 举报
回复
Object O = new Object();

因为上面这句!

因为这样一来,每个线程都有自己的Object对象,各自锁各自的,也就谈不上同步了

需要把O对象放出到线程外,确保该对象只能被生成一次,这样就能所有的线程都共享该对象了。

BTW,在Java中,一般用一个byte[0]的数组来替代一个Object

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧