package thread;
public class synchronization {
public static void main(String[] args){
Thread thread1=new Thread(new MySynchronized());
Thread thread2=new Thread(new MySynchronized());
thread1.start();
thread2.start();
}
}
class MySynchronized implements Runnable{
Object object=new Object();
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (object) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"gogogo");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"gameover");
}
}
}
}
public class synchronization {
public static void main(String[] args){
Thread thread1=new Thread(new MySynchronized());
Thread thread2=new Thread(new MySynchronized());
thread1.start();
thread2.start();
}
}
class MySynchronized implements Runnable{
Object object=new Object();
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (object) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"gogogo");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"gameover");
}
}
}
}