ashwin bhawsar

Ranch Hand
+ Follow
since Mar 16, 2011
ashwin likes ...
Eclipse IDE
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ashwin bhawsar

What are the rules/restructions/scenarios for how & where we can and cannot use the 'super' keyword.
8 years ago
Ok, got what the problem is.

One way to solve this problem is to swap thread calling

So now this is what will happen :
Thread_2 will start first -> enter sync method 'decreaseCount()' -> go in a wait state and wait for someone to notify -> release the lock for other thread.
Thread_1 will start -> enter the sync method 'increaseCouint()' -> acquire the lock -> complete the execution -> call the notify() -> release the lock.
Thread_2 which is waiting now will be notified -> it will execute the remaining code.

Thanks for your time.
Ok, so you are saying that Thread_1 completes its execution, calls notify() and after this Thread_2 starts the execution and waits and has no one to notify it.

But what is i make Thread_1 sleep for some time and allow Thread_2 to execute ?
I tried this but still doesn't works.

I have a Counter Class with two methods : increaseCount() and decreaseCount().
I also have two threads. Thread_1's run calls the increaseCount() and Thread_2's run calls the decreaseCount().
Both threads share a common instance of Counter class.

Now, what i want is Thread_2 to execute decreaseCount() only after Thread_1 has finished executing the increaseCount().
( i know this can be done with join() method, but want to achieve the same thing using wait() and notify() )

I have made the Thread_2 to wait till the Thread_1 completes its execution. After Thread_1 finishes its execution it call the notify() on the shared object.
But the problem is that Thread_2 keeps on waiting even after Thread_1 notifies it. Why ? am i missing something ?

As per one of the Rules of Constructors :

Every constructor has, as its first statement, either a call to an overloaded constructor ( this() ) or a call to superclass constructor( super() )

I know that compiler always insert super() as 1st statement, then what about this() when is that inserted by the compiler ?
9 years ago
I was going through the "Invoking a Superclass version of an Overriden Method" topic on page no 107 of SCJP (Exam 310 - 065 ) book.

If i am not wrong it says that you can only invoke the super-class methods using "super" keyword from its overriden version.
Also there is a note at the end that says " Note: Using super to invoke an overridden method only applies to instance methods. "

But i am able to invoke all kinds of methods using "super" keyword. I can even invoke static, final methods of the super class.
Someone can please explain me what is happening.

9 years ago
So you mean non-sync methods will run even if there is lock on that instance ?
Locking concept doubt: Thread calling a method on an instance even if another thread has a lock on that same instance ?

I have a Student class with 2 non-static methods, one synchronized and another non-synchronized method.
I am creating 2 threads (Thread_A & Thread_B) using the same instance of the Student class.
Thread_A will always call the synchronized method and Thread_B will always call the non-synchronized method.

Suppose Thread_A starts before Thread_B and enters the synchronized method. Now Thread_A has acquired a lock on the instance of the Student class and will not realease it untill it finishes the execution. My doubt is that even if the Thread_A has a lock on the student instance, why Thread_B is able to call the Non-Sync method on the same student instance before Thread_A releases the lock on that instance.

Why locking works only if both methods are synchronized.

The approach was wrong. The solution to the problem can be achieved using the wait() and notify() methods.

I had put an infinite loop in Thread_C to continually monitor if the Thread_B has written some value, but doing so Thread_C was not exiting the run method and so not going "dead".
As Thread_B had join over Thread_C, Thread _B could not go further unless Thread_C was not over and this was not happening.

To achieve the goal i have used the wait() and notify() methods.
Below is a new code that will make clear how to achieve the goal using wait() and notify() methods.
( Note : The code now does not take any input from the user, instead the producer produce the values)

I want to keep thread_C running, but doing so thread_B will never move forward as thread_C's run is in while loop, so it will never die.
Currently iam learning Threads topic from Kathy Sierra.
I am trying to write a typical producer-consumer code that makes use of the join() method of Thread.

This is what i want to accomplish :
1> There will be 3 threads Thread_A, Thread_B and Thread_C.

2> Thread_A's job is to continuously take input from the User from console and store it in an ArrayList.
The input will be in form of positive integer numbers ( eg: 2, 3, 100, 50 ....).

3> Thraed_B's job is to iterate through the array list and read these values one-by-one and store it in a variable.
Thraed_B will not read the next values unless Thread_C has finished its job.( Thread_B has join() on Thread_C)

4> Thread_C's job is to print '#' character in a File as per the values in the variable. So if the
values is say 20, then Thread_C will print '#' 20 times in the file.

To summarize, if the user has entered (20, 5, 10, 50) then Thread_C should print '#' 20 times, then 5 times then 10 times.....

I am not getting where to start the Thread_C.

Thanks a lot for guiding me to the right forum.
9 years ago