• Post Reply Bookmark Topic Watch Topic
  • New Topic

join() method  RSS feed

 
zohreh talebi
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to do this task and i have a problem.
there is one array of constant length and many threads are trying to fill this array simultanously. Since this array is shared among these threads you need to apply synchronization in right region.

After the array is filled completely the average of the array elements should be calculated and printed in the console.

this is my question:
1)filling array is my region synchronization .i don't underestand , why i should use join method when i have synchronization .because synchronization means one thread should do it's work and when it's work is finished another
thread can come.am i true?

thanks
zohreh
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

"join()" lets you wait for a Thread to exit completely. Imagine a main program that starts ten worker threads, where each one has a job to do. When all ten are done, the main program wants to print "All done!" and then exit. The main program could call join() on all ten threads, in a loop. When all ten join() calls have returned, the main program knows all the work is done, so it can print a message.

But you are right -- most uses of synchronization do not involve calling join(); join is not really related to synchronization at all.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a homework assignment to me.

Without knowing why you want to use join() and what the instructor is looking for, it's difficult to know how to respond.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!