package com.multithreading;
public class SyncronizationModes implements Runnable{
SharedResource1 sr = new SharedResource1();
public static void main(String args[]) {
SyncronizationModes rn = new SyncronizationModes();
Thread t1 = new Thread(rn,"FirstThread");
Thread t2 = new Thread(rn,"SecondThread");
t1.start();
t2.start();
}
public void run(){
sr.test();
sr.testAgain();
synchronized (this) {
sr.testNew();
}
}
}
class SharedResource1{/*
SYNCRONIZED BLOCK
*/
public void test() {
synchronized(this){
for(int i=0;i<10;i++){
System.out.println("Thread accessing the resource is..................."+Thread.currentThread().getName()+"value of i is::::"+i);
}
}
}
public synchronized void testAgain() {
/*
* SYNCRONIZED METHOD
*/
for(int i=0;i<10;i++){
System.out.println("Thread accessing the New resource is..................."+Thread.currentThread().getName()+"Thread is accessing the shared resource for ::::"+i+"time");
}
}
/*
*
* SYNCRONIZED RESOURCE
*/
public void testNew() {
for(int i=0;i<10;i++){
System.out.println("Thread accessing the New resource is..................."+Thread.currentThread().getName()+"------------------> ::::"+i+"time");
}
}
}
No comments:
Post a Comment