we can synchronize the functions in a class, or we can lock its object in the thread, to create thread safety on a object.
class DBresource {
synchronized public void dosomething() throws InterruptedException {
...
...
}
}
OR
class MyThread extends Thread {
public void run() {
synchronized (r) {
r.dosomething();
}
}
Which is advisable and why?
Further, i could lock on the class DBResource too. How would that be different?
OR, use a dedicated Lock object. Is that discouraged or prefered and why?
Thanks.