This example shows:
Synchronization at class level and Object level
If Synchronization is Class Level than i.e. Synchronization(CommonResource.class) than for all threads with new instance of the class for which lock is obtained the value/output will be same.
While if lock is instance level i.e. synchronized (this) than output will be different
Alternatively; The Monitor Lock that is associated by default witheach class in java can be used as an alternative, thus:
private Object lock = new Object();
synchronized (Object.class)
equivalent to
Synchronization(CommonResource.class)
and
private Object lock = new Object();
synchronized (lock)
equivalent to
Synchronization(this)
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class WaitNotifyExample {
public static void main(String args[]) {
SharedResource sr = new SharedResource();
Thread t1 = new Thread(sr, "1");
Thread t2 = new Thread(sr, "2");
Thread t3 = new Thread(sr, "3");
Thread t4 = new Thread(sr, "4");
Thread t5 = new Thread(sr, "5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class SharedResource implements Runnable {
// CommonResource cr = new CommonResource();
public void run() {
new CommonResource().getCount();
}
}
class CommonResource {
public void getCount() {
// System.out.println("Thread--" + Thread.currentThread().getName());
synchronized (CommonResource.class) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("Thread--"
+ Thread.currentThread().getName() + " i is " + i);
}
} finally {
// lock.unlock();
}
}
}
}
Synchronization can further be achieved by using java.util.concurrent.locks package Lock api as follows:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class WaitNotifyExample {
public static void main(String args[]) {
SharedResource sr = new SharedResource();
Thread t1 = new Thread(sr, "1");
Thread t2 = new Thread(sr, "2");
Thread t3 = new Thread(sr, "3");
Thread t4 = new Thread(sr, "4");
Thread t5 = new Thread(sr, "5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class SharedResource implements Runnable {
CommonResource cr = new CommonResource();
public void run() {
cr.getCount();
}
}
class CommonResource {
private final Lock lock = new ReentrantLock();
public void getCount() {
// synchronized (Object.class) {
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println("Thread--"
+ Thread.currentThread().getName() + " i is " + i);
}
} finally {
lock.unlock();
}
// }
}
}
Synchronization at class level and Object level
If Synchronization is Class Level than i.e. Synchronization(CommonResource.class) than for all threads with new instance of the class for which lock is obtained the value/output will be same.
While if lock is instance level i.e. synchronized (this) than output will be different
Alternatively; The Monitor Lock that is associated by default witheach class in java can be used as an alternative, thus:
private Object lock = new Object();
synchronized (Object.class)
equivalent to
Synchronization(CommonResource.class)
and
private Object lock = new Object();
synchronized (lock)
equivalent to
Synchronization(this)
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class WaitNotifyExample {
public static void main(String args[]) {
SharedResource sr = new SharedResource();
Thread t1 = new Thread(sr, "1");
Thread t2 = new Thread(sr, "2");
Thread t3 = new Thread(sr, "3");
Thread t4 = new Thread(sr, "4");
Thread t5 = new Thread(sr, "5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class SharedResource implements Runnable {
// CommonResource cr = new CommonResource();
public void run() {
new CommonResource().getCount();
}
}
class CommonResource {
public void getCount() {
// System.out.println("Thread--" + Thread.currentThread().getName());
synchronized (CommonResource.class) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("Thread--"
+ Thread.currentThread().getName() + " i is " + i);
}
} finally {
// lock.unlock();
}
}
}
}
Synchronization can further be achieved by using java.util.concurrent.locks package Lock api as follows:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class WaitNotifyExample {
public static void main(String args[]) {
SharedResource sr = new SharedResource();
Thread t1 = new Thread(sr, "1");
Thread t2 = new Thread(sr, "2");
Thread t3 = new Thread(sr, "3");
Thread t4 = new Thread(sr, "4");
Thread t5 = new Thread(sr, "5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class SharedResource implements Runnable {
CommonResource cr = new CommonResource();
public void run() {
cr.getCount();
}
}
class CommonResource {
private final Lock lock = new ReentrantLock();
public void getCount() {
// synchronized (Object.class) {
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println("Thread--"
+ Thread.currentThread().getName() + " i is " + i);
}
} finally {
lock.unlock();
}
// }
}
}
No comments:
Post a Comment