aspose file tools*
The moose likes Threads and Synchronization and the fly likes Help with thread synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Help with thread synchronization" Watch "Help with thread synchronization" New topic
Author

Help with thread synchronization

Justin Runner
Greenhorn

Joined: Jan 07, 2013
Posts: 5
Hello Everyone,
I have tried reading up on Thread Sync which the basics I'm pretty sure I understand. In my project I have 2 arrays of thread objects, we will say Aobj1 and Aobj2, on execution of my project the first thing I do is create Aobj1 and start the threads. Afterwards my project creates separate threads and adds them to Aobj2, during which time Aobj1's threads are polling Aobj2 to see if any threads are available. If there are, then 1 of the threads running under Aobj1 grabs the new thread from Aobj2 array and starts the new thread. (Hope you are still with me)

The problem I am having is, how do I synch these up and what am I synching on? (below is code to help) Also I'm not a guru when it comes to Java so some stuff may come over incorrectly (java terminology) I do apologies for that in advance. I also noticed the post doesn't keep the format either, sorry about that too..

So just to rehash here, I am not sure where or how to get everything in sync, do I need to sync the secondThreadList with anything or can those run without? I am pretty sure syncing Main with myThreads is a must because I get the following error (java.lang.IllegalMonitorStateException) which I know means the threads are not locked into my Main (I am guessing) due to the wait();

Also I have to use wait(), notifyAll() in this project.

Thanks for the help
Justin


// array that holds secondary threads //
private ArrayList<MYRunnable> secondThreadList = new ArrayList<MYRunnable>();

Main Constructor:
// in my main constructor I init 6 threads. MYThread's constructor starts the thread
for(i = 0; i < 6; i++) {
myThreads[i] = new MYThread(secondThreadList, Integer.toString(i));
}

Main:
// I have a loop that generates separate threads and adds them to an array, these are NOT started at creation time
myrunnable = new MYRunnable();
secondThreadList.add(myrunnable);

// once the new thread is in the array I notify all of the running threads which will check the array and pull the new thread off and start it//
notifyAll();


MyThread:
// in my run method I have the following, which polls the thread array and if its 0 then it waits, otherwise it will execute the new thread
while(mysecondThreadList.size() == 0)
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Running request in thread: " + threadId);
MYRunnable runnable = mysecondThreadList.get(0);
mysecondThreadList.remove(0);
runnable.myrun.start(); // myrun is the name of thread variable

Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Your questions and examples are very confusing. I think the first step is to realize that Runnable and Thread are two very different things. Threads are processes which are capable of executing Runnables. In your problem description you speak of arrays of threads, but in your example you have statements like these:



Is your array holding threads or runnables? Perhaps resolving this distinction will help you clarify the problem and either figure out the answer or at least ask more clear questions.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
I think whatever we discuss may be of benefit to others, so I am replying to your private message here.

Your homework assignment is definitely about threads and runnables. However, I believe you are confusing the two. In the assignment, you are asked to enhance the ATM Machine functionality you implemented in some previous assignment by making it multithreaded. If I understand the write-up correctly, you currently have a client/server implementation where each client can ask the server to deposit, withdraw, etc. The server is currently single-threaded, in other words the one thread which listens on the socket for client requests will only process them one at a time, sequentially. If two clients request to do anything, the server will take the first request, process it completely while the second client waits, then take the second request, process it completely, etc. The point of your assignment is to parallelize the processing of the requests. So the main server thread will take the request from the first client, hand it off to another thread for processing and immediately take the second request and hand it off to another thread for processing, etc. thereby speeding up the whole system.

You can think of the requests as runnables, work items, pieces of work, etc., and the things that actually process them in parallel as threads. I think the assignment calls for the main server thread to just dump the requests on a queue (some collection). This will be the queue of runnables, things to get done. There will also be a pool (another collection) of threads, which sit there waiting for work to do (this is why you are told to create them, start them and put them in a wait state). Whenever the main server thread gets a request and dumps it into the work queue it notifies the waiting threads that there is some new work to do. One of the waiting threads wakes up, picks the item off the queue and processes it.

Finally, you will need to make your current, single threaded, code safe for multithreading. Anything that might be accessed by multiple threads in parallel, must now be protected from race conditions. Some operations will probably need to be made atomic.
Justin Runner
Greenhorn

Joined: Jan 07, 2013
Posts: 5
That's fine, I PMed that private link that's why.

So knowing the background on this project, I do have a single socket/thread project. One of the issues I am having is the information I read, Threads and Runnables are basically the same I think? I believe Runnables resides inside the Thread class itself? Maybe? In my project the work queue would be a Runnable? This is what I wasn't sure about. Also the Thread collection will wake (from a notifyall) and execute the Runnable on the queue? I am making sure I understand this correctly.

So 1 Thread object and 1 Runnable object, the other issue is what or how would I synchronize everything? Due to I have to use wait()/notifyall() I have to use synchronized also, this is where my project is failing at.

Thanks again
Justin
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Thread and Runnable are very different things. Even in the java language itself, Thread is a class and Runnable is an interface, defined separately. It is true that a Thread class can be instantiated with a Runnable, or it can be instantiated without it and simply use its own run method to perform work. If you take a real life analogy, you can think of the Runnable as laundry and Thread as the washing machine.

In your case, the work queue would not BE a Runnable, it would be a collection of Runnables (things to do).

You will need to use synchronization (synchronized) not just because you cannot call wait() on anything that is not under synchronization, but also because you need correct concurrency control. For example, I could imagine something like this:



Also, I don't think you want 1 Thread since you are trying to improve throughput - you should create several threads, potentially as many as the system can handle, and you should make this a configurable parameter to your program. You will definitely not create just one Runnable, you will create a Runnable for every client request, so there will be a lot of those.
Justin Runner
Greenhorn

Joined: Jan 07, 2013
Posts: 5
So i have 3 pieces here:

Server:
Which defines the Threads and Runnables collections



Also in the Server I have code that will dynamically create the runnable and place it onto the workQueue.


And in my thread class I have the following:


The question I have is, I'm not sure if the synchronize is correct and what am I synchronizing on? Is it the workQueue or the threadQueue? Also do I need to do a sync in the runnables class?

Thanks
Justin




Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
It seems to me that you need to learn this stuff more thoroughly than I can explain in this piecemeal fashion. There is a great book by Brian Goetz, "Java Concurrency in Practice" which really explains and teaches this stuff thoroughly. I feel I might actually be doing you a disservice by trying to help explain this in pieces while you still lack the overall understanding of the issues.

One example of this is your confusion about the core concept of the java monitor objects. If you synchronize on some object, let's say x ...

... the jvm will serialize access to that code block among multiple threads - only the thread "holding" the object may proceed and only one thread may hold the object at a time. But it doesn't end there, because wait() and notify() are also arranged around this monitor object, so any thread which will execute x.wait(), will, first of all, have to be holding x, since the compiler will not let you write x.wait() outside of a block synchronized on x, will then release x and go into a wait queue focused around x, in other words, waiting for x.notify() to be called. If x.notify() is called it will wake up and proceed, but if y.notify() is called it will continue waiting.
Justin Runner
Greenhorn

Joined: Jan 07, 2013
Posts: 5
Well I guess the confusing part here is, I have Threads and Runnables, and the Threads are waiting, so I am assuming that this collection is what I am notifying. Also from your example above your syncing on A object, but I am using a collection of Objects. Can you sync on a collection or is just on individual objects. Your correct on my indepth understanding of Java and my background going forward will not be in programming, I have a project, inwhich I was hoping to find help online. Or better yet a more clarification on how this part of Java works. If you or anyone else can help with this I would appreciate it. I do appreciate the help so far, I think the confusing part is I have 4 intertwined objects and that is the confusing part of this for me I believe.

Justin
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
In java a collection is an Object - everything is an Object - so you can lock on the collection.
Justin Runner
Greenhorn

Joined: Jan 07, 2013
Posts: 5
Ok I was just making sure of that, most examples show 1 object. I understand that we are syncing on the workQueue so that our data doesn't get corrupt due to the multiple threads. The last issue I see is, in the Thread object we have our wait(), due to we want to wait til there is work to do. If in the Thread object itself we are accessing our workQueue then we have to sync that also for the same reasons. My question is, if I am doing the following:



How can I get a lock on the threads, without I would receive a java.lang.IllegalMonitorStateException.

Again I do appreciate all the help and thank you again!!

Justin

Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
When you do something like this:



it is equivalent to saying this:


so you are actually synchronizing on the instance of the class in which you are writing the method. You don't want to be doing that, you want to synchronize on the collection, not on the instance of the whole class.
 
Consider Paul's rocket mass heater.
 
subject: Help with thread synchronization