62,623
社区成员
发帖
与我相关
我的任务
分享
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;
}