public static void main(String[] args) {
final MyThread_synchronized myThread = new MyThread_synchronized();
// 同步
//new Thread(new MyThread_synchronized()).start();
new Thread(myThread).start();
/* 创建匿名线程对象 */
Thread td = new Thread(new Runnable() {
public void run() {
synchronized (myThread) {//要实现同步也要用MyThread_synchronized的实例对象做为同步锁对象
for (int i = 0; i < 5; i++) {
System.out.println("匿名线程正在运行" + i);
}
}
}
});
td.start();
}
}
class MyThread_synchronized implements Runnable {
public void run() {
//synchronized (new Object());
synchronized (this)//当前对象做为锁对象
{
for (int i = 0; i < 50; i++) {
System.out.println("同步:MyThread_implements线程正在运行" + i);
}
}
}
}