线程同步synchronized(m)
package com.veer;
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException{
Object obj = new Object();
for (int i = 0; i < 10; ++i) {
new Thread(new MyThread(i,obj)).start();
}
}
}
class MyThread implements java.lang.Runnable {
private int threadId;
private Object m;
public MyThread(int id,Object m) {
this.threadId = id;
this.m= m;
}
public void run() {
// TODO Auto-generated method stub
synchronized(m){
for (int i = 0; i <100; ++i) {
System.out.println("Thread ID: " + this.threadId + " : " + i);
}
}
}
}