Multithread program in java - java

My code gives me a problem.
My code throws IllegalMonitorStateException in setStr which is in Hoge class.
I change Hoge.class into this in setStr. My code correctly finished!
But why did it finish normally?
public class Sample {
static Hoge gh = new Hoge();
static Hoge gh2 = new Hoge();
public static void main(String[] args) {
new Thread() {
private Hoge h2 = gh;
public void run() {
System.out.println("start initialize");
h2.setStr("BazzBazz");
System.out.println("end initialize");
System.out.println("start thread 1");
System.out.println(h2.getStr("thread-1"));
System.out.println("end thread 1");
}
}.start();
new Thread() {
private Hoge h2 = gh2;
public void run() {
System.out.println("start thread 2");
System.out.println(h2.getStr("thread-2"));
System.out.println("end thread 2");
}
}.start();
}
}
class Hoge {
private String fuga = "fugafuga";
public void setStr(String str) {
synchronized(Hoge.class) { //<-HERE ! change "Hoge.class" into "this".
fuga = str;
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
String getStr(String suffix) {
synchronized(Hoge.class) {
return suffix+ fuga;
}
}
}

YoursetStr method should be like this:
public void setStr(String str) {
synchronized(Hoge.class) { //<-HERE ! change "Hoge.class" into "this".
fuga = str;
try {
Hoge.class.wait();//call wait on Hoge.class
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
You should use Hoge.clas.wait() instead of wait(). Why?
Because , as specified in oracle documentation about wait() :
This method should only be called by a thread that is the owner of
this object's monitor.
i.e a thread can't invoke a wait on an object until it owns that object's lock. Otherwise it will throw IllegalMonitorStateException . Here , you are acquiring lock on object of Class of Hoge(i.e Hoge.class) called as class level lock , but was calling wait on the current object of Hoge(this). So it was leading to IllegalMonitorStateException. That's why your code was working fine when you acquiring lock on the current object (this) because wait() in that case was called on the current object (this) itself.

Because this in gh and gh2 are different, this is an instance of Hoge.
So when use Hoge.class, there is noly one synchronized lock, rather than using this which will use two different lock.

Related

Trying to Get Guarded Blocks to Work

I'm not understanding why my code is not working correctly. I expect the first Thread to wait for 4 seconds for the second Thread to set a shared boolean "joy" to true, then for the first Thread to print out "Joy has been achieved!".
When I run the code, I get this output:
"No Joy Yet..."
"Notifying Joy"
Then it freezes up and doesn't continue. If my understanding is correct, the notifyAll() method which is called from my notifyJoy() method should wake t1 up from its wait() and then, since the shared static boolean variable joy is now true, "Joy has been achieved!" should print to the console.
I'm working from Oracle's "The Java Tutorial", Chapter 13: here is a link to the specific section: Java Tutorial Website. I'm going off of what they have and making a little example but I can't seem to figure out what I'm doing wrong. Any help would be appreciated. Here is a complete copy of my code for your reference:
public class JoyTime {
public static void main(String[] args) {
JoyRider j1 = new JoyRider(false);
JoyRider j2 = new JoyRider(true);
Thread t1 = new Thread(j1, "J1");
Thread t2 = new Thread(j2, "J2");
t1.start();
try {
Thread.sleep(4000);
}
catch (InterruptedException e) {}
t2.start();
}
}
class JoyRider implements Runnable {
private static boolean joy = false;
private boolean flag;
public JoyRider(boolean flag) {
this.flag = flag;
}
#Override
public void run() {
synchronized(this) {
if (flag) {
notifyJoy();
}
else {
while (!joy) {
System.out.println("No Joy Yet...");
try {
this.wait();
}
catch (InterruptedException e) {}
}
System.out.println("Joy has been achieved!");
}
}
}
public synchronized void notifyJoy() {
System.out.println("Notifying Joy");
joy = true;
notifyAll();
}
}
You are calling wait() and notifyAll() on different monitors, and more specifically on the built-in monitors of the two different JoyRider instances.
If you introduce a dedicated lock object:
private static final Object LOCK = new Object();
and change your run() method a little:
synchronized (LOCK) {
if (flag) {
System.out.println("Notifying Joy");
JOY = true;
LOCK.notifyAll();
}
else {
while (!JOY) {
System.out.println("No Joy Yet...");
try {
LOCK.wait();
}
catch (InterruptedException e) {}
}
System.out.println("Joy has been achieved!");
}
}
you should be able to see all the expected prints in the correct order.

Why doesn't it create a deadlock?

Please refer to the code below
package com.test;
public class DeadLock {
private void method1() {
synchronized (Integer.class) {
method2();
}
}
private void method2() {
synchronized (Integer.class) {
System.out.println("hi there");
}
}
public static void main(String[] args) {
new DeadLock().method1();
}
}
As per my understanding, the code in method2 should not be executed in any case, since method1 holds the lock on Integer.class and method2 tries to access the lock on Integer.class again. But to my surprise, the code runs fine and it prints "hi there" to the console. Can someone clarify?
Locks are owned by threads. If your thread already owns a lock, Java assumes that you don't need to acquire it a second time and just continues.
You'll get a deadlock if you start a second thread in method1() while holding the lock and the second thread executes the method method2().
If you prefer code, then synchronized works like this:
Lock lock = Integer.class.getLock();
boolean acquired = false;
try {
if(lock.owner != Thread.currentThread()) {
lock.acquire();
acquired = true;
}
...code inside of synchronized block...
} finally {
if(acquired) lock.release();
}
Here is code to demonstrate the deadlock. Just set runInThread to true:
package com.test;
public class DeadLock {
private void method1() {
synchronized (Integer.class) {
boolean runInThread = false;
if( runInThread ) {
Thread t = new Thread() {
#Override
public void run() {
method2();
}
};
t.start();
try {
t.join(); // this never returns
} catch( InterruptedException e ) {
e.printStackTrace();
}
} else {
method2();
}
}
}
private void method2() {
System.out.println("trying to lock");
synchronized (Integer.class) {
System.out.println("hi there");
}
}
public static void main(String[] args) {
new DeadLock().method1();
}
}
It seems you have misunderstood the concept.
A method never acquires a lock, the instance on which the method is invoked serves as a lock in case of synchronized method and in case of synced block the thread acquires the lock on specified object.
Here the instance acquires the lock on Integer.class and then it goes on to execute method2.
There is no case of deadlock as in your case thread continues for the execution of the method that you're calling inside method1. So there is no deadlock that happens.
your code is equivalent to:
synchronized (Integer.class) {
synchronized (Integer.class) {
System.out.println("hi there");
}
}
if the thread acquired the lock and entered the first synchronized block it will have no problem accessing the 2nd
to produce a deadlock the call to method2 should be executed by a different thread.
synchronized (Integer.class) {
method2();
}
when you calling this method2(); then its not giving lock to any kind of mehtod its continues goes to the method that you are calling means this.
private void method2() {
synchronized (Integer.class) {
System.out.println("hi there");
}
}
and after completing its returning. so there is no case of dead lock. hope this explanation helps.
As already said, one thread can access more than one synchronised blocks when no other thread already blocks it. In that situation the same thread can reenter synchronised block because it already holds it from method1.
To cause the deadlock you have to use two thread at least and two different locks. It have to access two locks in the reverse order. Check out that code:
private void method1() throws InterruptedException
{
synchronized (Integer.class)
{
System.out.println(Thread.currentThread().getName() + " hi there method 1");
Thread.sleep(1000);
method2();
}
}
private void method2() throws InterruptedException
{
synchronized (Double.class)
{
System.out.println(Thread.currentThread().getName() + " hi there method 2");
Thread.sleep(1000);
method1();
}
}
public static void main(String[] args) throws InterruptedException
{
new Thread()
{
#Override
public void run()
{
try
{
new DeadLock().method1();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}.start();
new DeadLock().method2();
}

interrupt one thread inside another thread's run method in Java

I was reading this post and the suggestions given to interrupt one thread from another is
" " " Here are a couple of approaches that should work, if implemented correctly.
You could have both threads regularly check some common flag variable (e.g. call it stopNow), and arrange that both threads set it when they finish. (The flag variable needs to be volatile ... or properly synchronized.)
You could have both threads regularly call the Thread.isInterrupted() method to see if it has been interrupted. Then each thread needs to call Thread.interrupt() on the other one when it finishes." " "
I do not understand how the second approach is possible that is using Thread.isInterrupted().
That is, how can Thread-1 call Thread.interrupt() on Thread-2.
Consider this example, in the main method I start two threads t1 and t2. I want t1 to stop t2 after reaching certain condition. how can I achieve this?
class Thread1 extends Thread {
public void run(){
while (!isDone){
// do something
}
} //now interrupt Thread-2
}
class Thread2 extends Thread {
public void run(){
try {
while(!Thread.isInterupted()){
//do something;
}
catch (InterruptedExecption e){
//do something
}
}
}
public class test {
public static void main(String[] args){
try {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The context of this is that you are trying to implement your scheme using thread interrupts.
In order for that to happen, the t1 object needs the reference to the t2 thread object, and then it simply calls t2.interrupt().
There are a variety of ways that t1 could get the reference to t2.
It could be passed as a constructor parameter. (You would need to instantiate Thread2 before Thread1 ...)
It could be set by calling a setter on Thread1.
It could be retrieved from a static variable or array, or a singleton "registry" object of some kind.
It could be found by enumerating all of the threads in the ThreadGroup looking for one that matches t2's name.
public class test {
private static boolean someCondition = true;
public static void main(String[]args){
Thread t2 = new Thread(new someOtherClass("Hello World"));
Thread t1 = new Thread(new someClass(t2));
t2.start();
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static class someClass implements Runnable{
Thread stop;
public someClass(Thread toStop){
stop = toStop;
}
public void run(){
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(someCondition && !stop.isInterrupted()){
stop.interrupt();
}
}
}
}
static class someOtherClass implements Runnable{
String messageToPrint;
public someOtherClass(String s){
messageToPrint = s;
}
public void run(){
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(messageToPrint);
}
}
}
}
You could consider the use of Future interface. It provides a cancel() method.
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
Playing with interruption makes your life unnecessarily hard. Besides the fact that your code must know the threads, interruption does not provide any context information about the reason of the interruption.
If you have a condition that is shared by your code possibly executed by different threads, just encapsulate that condition into an object and share that object:
public class Test {
public static void main(String[] args) {
Condition c=new Condition();
new Thread(new Setter(c)).start();
new Thread(new Getter(c, "getter 1")).start();
// you can simply extend it to more than one getter:
new Thread(new Getter(c, "getter 2")).start();
}
}
class Getter implements Runnable {
final Condition condition;
final String name;
Getter(Condition c, String n) { condition=c; name=n; }
public void run() {
while(!condition.isSatisfied()) {
System.out.println(name+" doing something else");
try { Thread.sleep(300); } catch(InterruptedException ex){}
}
System.out.println(name+" exiting");
}
}
class Setter implements Runnable {
final Condition condition;
Setter(Condition c) { condition=c; }
public void run() {
System.out.println("setter: doing my work");
try { Thread.sleep(3000); }
catch(InterruptedException ex){}
System.out.println("setting condition to satisfied");
condition.setSatisfied();
}
}
class Condition {
private volatile boolean satisfied;
public void setSatisfied() {
satisfied=true;
}
public boolean isSatisfied() {
return satisfied;
}
}
The big advantage of this encapsulation is that it is easy to extend. Suppose you want to allow a thread to wait for the condition instead of polling it. Taking the code above it’s easy:
class WaitableCondition extends Condition {
public synchronized boolean await() {
try {
while(!super.isSatisfied()) wait();
return true;
} catch(InterruptedException ex){ return false; }
}
public synchronized void setSatisfied() {
if(!isSatisfied()) {
super.setSatisfied();
notifyAll();
}
}
}
class Waiter implements Runnable {
final WaitableCondition condition;
final String name;
Waiter(WaitableCondition c, String n) { condition=c; name=n; }
public void run() {
System.out.println(name+": waiting for condition");
boolean b=condition.await();
System.out.println(name+": "+(b? "condition satisfied": "interrupted"));
}
}
Without changing the other classes you can now extend your test case:
public class Test {
public static void main(String[] args) {
WaitableCondition c=new WaitableCondition();
new Thread(new Setter(c)).start();
new Thread(new Getter(c, "getter 1")).start();
// you can simply extend it to more than one getter:
new Thread(new Getter(c, "getter 2")).start();
// and you can have waiters
new Thread(new Waiter(c, "waiter 1")).start();
new Thread(new Waiter(c, "waiter 2")).start();
}
}

notify giving IllegalMonitorStateException

I have very poor knowledge on Multithreading.
I am expecting below Program to run perfectly, but it is not working and produces below Exception.
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at com.onmobile.client.D.callD(Deadlock.java:76)
at com.onmobile.client.B.run(Deadlock.java:50)
at java.lang.Thread.run(Unknown Source)
Java File
public class Deadlock {
C c = new C();
D d = new D();
public static void main(String[] args) {
new Deadlock();
}
public Deadlock() {
A a = new A(d,c);
B b = new B(d,c);
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}
}
class A implements Runnable{
D dObj;
C cObj;
A(D obj, C obj1){
this.dObj = obj;
this.cObj = obj1;
}
#Override
public void run() {
cObj.callC(dObj);
}
}
class B implements Runnable{
D dObj;
C cObj;
B(D obj, C obj1){
this.dObj = obj;
this.cObj = obj1;
}
#Override
public void run() {
dObj.callD(cObj);
}
}
class C{
public synchronized void callC(D dObj){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
dObj.callD1();
}
public synchronized void callC1(){
}
}
class D{
public synchronized void callD(C cObj){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
cObj.callC1();
cObj.notify();
}
public synchronized void callD1(){
}
}
I thought inside callC() method, object of class C ie cObj goes in waiting state and meanwhile control will go to callD() method and there it invokes cObj.notify();
So, this will awake waiting threads waiting on object cObj.
but it is giving me Exception.
I think solution to my problem can be :
Java: IllegalMonitorStateException on notify()
but I am not understanding it correctly.
Please guide me where I am going wrong.
when calling object.notify, you must be holding the the lock for that exact object, ie:
synchronized(cObj) {
cObj.notify();
}
you should also wrap your wait call in a similar synchronized block:
synchronized(cObj) {
cObj.wait()
}
I suggest you read more about it in the Java tutorial:
http://docs.oracle.com/javase/tutorial/essential/concurrency/
If you want to notify or wait on an object, your thread must own the monitor of the object you act on.
public synchronized void callD(C cObj){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
cObj.callC1();
cObj.notify();
}
In this code section you synchronize on the instance of class D, as synchronized methods always obtain the monitor of the object they "live" on. But to be able to use cObj.notify() you have to obtain the monitor of the cObj instance, e.g. by doing
synchronized(cObj) {
cObj.notify();
}
notify on an Object should be called only after taking lock or synchronizing on that object. So in your case what you need is:
class D{
public void callD(C cObj){ //synchronized not required here
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronize(cObj) { //synchornize here on cObj
cObj.callC1();
cObj.notify();
}
}
}
Note: in case you are change your code to synchornize callD and synchroniz(cObj) make sure you on deadlock scenario. IMHO only synchroniz(cObj) should suffice.
To call notify() on an object you need to you own this object's lock. In this case to call
cObj.notify();
you need to wrap it into syncronized block like this:
syncronized (cObj)
{
cObj.notify();
}

Second Thread does not give an output (java)

class firstThread extends Helper1
{
Thread thread_1 = new Thread(new Runnable()
{
#Override
public void run() {
try {
for (int i = 1; i <= 20; i++) {
System.out.println("Hello World");
Thread.sleep(500);
if (i == 10) {
Notify();
Wait();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
class secondThread extends firstThread
{
Thread thread_2 = new Thread(new Runnable()
{
#Override
public void run() {
// TODO Auto-generated method stub
try {
Wait();
for(int i = 1; i<=20; i++)
{
System.out.println("Welcome");
Thread.sleep(100);
}
Notify();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
class Helper1
{
public synchronized void Wait() throws InterruptedException
{
wait();
}
public synchronized void Notify() throws InterruptedException
{
notify();
}
}
public class InheritanceClass {
public static void main(String[] args)
{
Thread f = new Thread(new firstThread().thread_1);
Thread s = new Thread(new secondThread().thread_2);
f.start();
s.start();
}
}
Only the first Thread has an output. Please try my code. I don't know why it happens.
The second thread does not give output, I suppose it's because of Wait() in the secondThread, I don't know what to do.
The problem is with the following code:
class Helper1
{
public synchronized void Wait() throws InterruptedException
{
wait();
}
public synchronized void Notify() throws InterruptedException
{
notify();
}
}
Above, the wait() and notify() calls are equivalent to this.wait() and this.notify(). However, thread1 and thread2 are separate objects so they are not ever going to communicate via this method.
In order for communication to occur, you need a shared lock object. For example:
Object lock = new Object();
firstThread = new firstThread(lock);
secondThread = new secondThread(lock);
and synchronizations like:
void wait(Object lock) {
synchronized(lock) {
lock.wait();
}
}
void notify(Object lock) {
synchronized(lock) {
lock.notify();
}
}
Disclaimer: I would never do this personally, however it does answer the OP's question.
This code is really confusing, which is making it hard to see the underlying problem.
You should never start a class with a lower-case letter since it makes it look like a method/field name (e.g. firstThread).
I'm pretty sure Wait and Notify have no reason to be synchronized.
Why does secondThread inherit from firstThread??? Actually, why do you have those two classes at all? You should just make an anonymous inner class from Helper1 or something.
Anyway, the problem is that when you call Notify() in thread1 it notifies itself, not thread2.

Categories

Resources