0

If I wrote the following classes:

public class Thread1 extends thread{ int num; OtherObject obj; boolean isFinished; public Thread1(int num, OtherObject obj){ this.num = num; this.obj = obj; isFinished = false; } public void run(){ this.startMethod(); } public synchronize void startMethod(){ obj.anotherMethod() if(isFinished !=true) wait(); isFinished = true; notifyAll(); } public class main{ public static void main (String[] args){ OtherObject other = new OtherObject(); Thread1 first = new Thread1(1, other); Thread1 second = new Thread1(2, other); first.start(); second.start(); 

*this code doesn't has logic meaning, but to represent the idea of synchronized, wait(), notify().

1) When will the synchronized method run synchronized. "first" and "second" are different objects, so will they be synchronized? or not? or because they have shared variable - OtherObject obj they will be synchronized?

2) When I write wait() lets say for thread "first" the object will go to sleep and wake up when?

only when another object from the same class has done the notify()? or also when any object in the program will call notify()? and if I write the notify in "OtherObject" class it will also make "first" object to wake up?

3) The "startMethod" is synchronized, because of it, when I start() the first and second threads then they do the run method and start the "startMethod".

The "startMethod" will be done as synchronized or not? maybe because it goes to "anotherMethod" (that is not synchronized method) inside or because it started from "run" that is not synchronized also? I didn't understand when method that is marked as synchronized will be synchronized and in which circumstances it will stop being synchronized?

1
  • Don’t try to make code which “doesn't has logic meaning” multi-threaded. It’s hard enough with code which has logic meaning. In your case your code seems supposed to wait until isFinished is true and then set isFinished to true. That doesn’t work in any way, regardless of multi-threading. Regarding synchronized, you are trying it from the wrong end. First you have to understand why you need synchronized at all. There’s good literature around Commented Jul 1, 2014 at 9:51

1 Answer 1

1

3) The method is synchronized, but only to that object instance, both threads will run it without blocking each other since they are 2 separate instances.

2) If a thread uses wait (you have to have a lock first), it goes into waiting mode and only start again when another thread with the same lock uses notify/nofityAll. If a thread with a different lock calls notify, nothing will happen to that specific thread.

Side node: notify tells a single thread to continue but its chosen at random so many people prefer to use notifyAll.

1) if you want to be able to sync on a sharable variable you are looking to use this:

var Object obj; //assign instance on constructor or something like that void method(){ synchronized(obj){ //sync code for obj instance } } 
Sign up to request clarification or add additional context in comments.

3 Comments

In the "//sync code for obj instance" until this code will finish no one will be able to approach that object?
also when inside "//sync code for obj instance" i call to other methods?
well, if there are other places with that object without the sync, they would be able to access it, but that sync code would only be executed one thread at a time and any other place that has the same synchronization pointing at that object. and yes, you can add any statement in it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.