Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

thread

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all..
I have a requirement..there are 4 people and each person hold a color balloon.. I need to ask them question at once.. But I need to print their answer only after 4 persons replied..

I created 4 threads to read answers..using thread names.. How I can store those answers and print after all threads excecutes..
 
Bartender
Posts: 4743
184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vivek,

well, if your program runs on the EDT, then I suggest you use 4 Swingworkers, and retrieve the answers in the 'done()' method.
This 'done()' method runs on the EDT as well, so you would not get any synchronisation problem here.

If you are not running from the EDT, then one possibility is: create, say, an ArrayLIst<String> (an Array would do fine as well),
start four threads, each writing to the arraylist once it has an answer. But for this writing, synchronize on this arraylist.
Then, for your main thread, join the four threads. You know then when there are four answers.

And then you could use executors, but that would be a bit overkill in this case.

Greetz,
Piet
 
vivek shankare gowda
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet Souris,

Thanks for your reply. I used synchronized arraylist and stored values in the list. But I do not know how to use executors to print the answers once all threads are executed. Can you help me how I can iterate the list to print the answers
 
Piet Souris
Bartender
Posts: 4743
184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,

suppose your threads are: t1, t2, t3, t4, and that you've just started all of them.

Then you could do something like:

This is a very basic scheme, and forget that I mentioned the term 'executor'.
If you want to know more about threads, and there is plenty more to know,
have a look at the Concurrency tutorial from Oracle:
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html

Last remark: don't use this scheme if you are running your main thread in the
EDT (i.e. if you use some GUI program with windows and buttons and JPanels and so on
that runs from the so called 'Event Dispatch Thread'), or you will encounter a freezing GUI,
if t1, t2, t3 and t4 take more than a fraction of a second to finish.

Greetz,
Piet
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may look like this demo:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic