• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Synchronization

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here i am working on thread communication , where two threads communicates using single pipe . I was successfully able to create a class which generates random number put in a pipe and other class which reads random number from pipes. I have also made use of thread notify and wait .. which generally works likes this ., if one class generates a number it waits and notify other thread that number has been generated read the number , after reading second thread notifies first thread that it has finished reading the data, generate next number and it goes to wait condition ..
Program what i have written generally works for small range (up to 10 numbers ) i made use of for loop (i<10), now i need this program to work for large range of numbers , replacing for loop with while would be great deal .. For large range of numbers its not working properly .. I need help in this ..

Thanks in advance ..
cheers

Here is the Code



 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shouib mohammed,

Welcome to CodeRanch!

Please UseCodeTags. Your code is not very tiny, and it becomes difficult to read it without proper coding style and coding tags. You can still edit that post.

I haven't gone through your code line-by-line yet, but I have a few points to make:

shouib mohammed wrote:Program what i have written generally works for small range (up to 10 numbers )


Well, either a program is thread-safe or it is not. A thread-safe program should work for large data as well (unless there are any memory issues - anyway there should not be any threading related issues).

shouib mohammed wrote:replacing for loop with while would be great deal


Why is it so?

shouib mohammed wrote:For large range of numbers its not working properly


Means what? It stops working? It works but corrupts the data? It throws exception? Please let us know exactly why do you think it's not working.

Besides this, it would be pretty nice code if you make a wise use of wait and notify methods instead of boolean variables like waiting and ready.

Secondly, unless you are not altering behavior of java.lang.Thread class in some radical way, general practice is to implement java.lang.Runnable interface instead of extending Thread class.

I hope this helps.
 
shouib mohammed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replay ..

I am new to this java programming . How to make program thread safe ?? any class or method for available ?? Well i want this program to work for large data so wants to replace for loop with while loop . It will work for large amount of range (range of 100 numbers) but not as expected (producer/ consumer) which i have mentioned earlier .
Thanks
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in that case, I would suggest:
1) Pick a descent Java book (not necessarily dedicate to threading, but it should contain at least a chapter for threading) and go through threading concepts.
2) Focus on synchronization and wait/notify.
3) In book itself, you'll get a good example of thread safe code (most probably a producer-consumer problem).

shouib mohammed wrote:How to make program thread safe ?? any class or method for available ??


The answer is not one-liner. Yes, wait and notify are widely used for synchronization, but there are other ways also (like using locks). Also, thread-safety heavily depends on how you use those methods.

I hope this helps.
 
shouib mohammed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i will go through the books . It would take time to re implement code and i am in between of the project so if you can help me out in modifying above code ,because its almost 90 percent correct .. just need a guidance to make it perfect .

Thanks
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this is NotACodeMill, and if you don't understand properly what and why we have to use specific methods, then it would be difficult to fix problems later.

Anyways, I would like to repeat:

Anayonkar Shivalkar wrote:

shouib mohammed wrote:For large range of numbers its not working properly


Means what? It stops working? It works but corrupts the data? It throws exception? Please let us know exactly why do you think it's not working.

 
shouib mohammed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For small range of numbers it works according to producer consumer , that is one class generates the data (produces) and generation it notifies for consumer class to consume the data producer class go to wait condition when consumer class consumes the data it notifies producer class to produce next data. This concept is working fine for small range . When considering large number ,the behavior is changing producer class generates data , does not waits for a condition either wait or notify it produces large amount of data before consumer class consumes the data, consumer class responds late , classes are not synchronized , they do not follow wiat notify mechanism .. they do not throw any error or corrupt data..
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as I already mentioned, if the code works for small data and not for large data, it is not thread safe.

I can clearly see that you are having 'while' loops over 'waiting' variable, but you are hard-coding it to false. Even more weird is - you are accepting it's value via constructors, and in run methods, you are simply ignoring it, making it false, and making sure that no thread will ever wait!

So, I would still suggest to go through synchronization, wait and notify concepts. If due to tight deadlines, if it is not possible to go through a book, please try to understand those concepts from your friends/colleagues.

I hope this helps.
 
Greenhorn
Posts: 20
Google Web Toolkit Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shouib mohammed,

Your program is not utilizing thread synchronization properly. Otherwise, it would work properly even for infinite number of iterations....

Say Threads A and B want to communicate with each other. They have a common data to share. They communicate through the common data. In your case, Pipe. A puts a number in the pipe and B reads it. So, you need to synchronize any code that tries to read the pipe or that tries to modify/write the pipe. And you need to use the data object(pipe) as the lock which would control access to the pipe. It is general practice to use shared data itself as lock and any read/write methods would be put in the data object class itself so that those methods can be synchronized.

Now, then, the waiting part, A should write a number into the pipe and call lock.notifyAll() to alert thread B. Here lock is your pipe object. Thread B in the meantime, was waiting by checking the pipe. While the pipe is empty, B should wait by calling lock.wait(). Something like " while(pipe.isEmpty()) pipe.wait(); " in an infinite loop. When pipe is not empty, it can read the pipe and then signal A to write next item by calling pipe.notifyAll(). Thread A also waits for pipe to become empty in a similar while loop as mentioned above, and when the pipe becomes empty it writes a new number into the pipe and signals B again.

Now, even after reading the above you wouldn't be able to code your program properly because, the above logic consists of certain code block that comes inside synchronized(pipe){} block. You should know which code lines have to be synchronized and which should be outside synchronized block. That is, basically the run methods of your two threads I'm talking about.

So, learn them first and then fix your problem. Good luck!
 
shouib mohammed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks prabakar your post helped me a lot ..
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shouib mohammed wrote:i want this program to work for large data so wants to replace for loop with while loop



This makes no sense. Anything you can do with a for loop, you can do with a while loop, and vice versa. The "size of the data" or number of iterations or whatever has nothing to do with the choice of for or while.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic