• 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

Java concurrency: create a thread for every object in a list and then use wait()

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i'm new.

I know this quesion is kinda strange, but I don't know how to do this in my own.

So: I have this List of Objects, and the trace of my homework says that I have to create a function that makes every object inside the list in wait(), so the thread waits on that object.
I know that 1 thread can menage 1 object (with wait()).

So do I have to create inside that function 1 Thread for every object inside the list??
If yes ok, but i'm very confused in "how to do that" ahah
 
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean this?
Assume you have 10 Runnable objects. You create 10 threads for these objects, one for each.
When 1 thread is doing a task, the rest of the 9 threads waits. Then, the first thread notifyAll threads to wake up from waiting state.

Reference:
https://howtodoinjava.com/java/multi-threading/wait-notify-and-notifyall-methods/
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possibility:

if you have a List of Callables, then create a Treadpool with just one Thread, and let that 'invokeAll' your list.

And welcome to the Ranch!
 
Filippo Restori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok ty guys, I will check some guides for thread pools so!
 
Filippo Restori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW for now i've done something like this, maybe you understand more than in words
 
Saloon Keeper
Posts: 15526
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really, before we can say anything about that code, we really need to see how the original homework problem was phrased. Can you quote it verbatim?
 
Filippo Restori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, the trace is very long, but the part of the fuction waitAll() is this: "the waitAll () method allows you to wait for any of the objects in the list to be signaled by notify () or notifyAll ()"

So I assumed that i had to create a thread for every object in the list and then use wait in every innerThread
 
Stephan van Hulst
Saloon Keeper
Posts: 15526
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Going by just that single line, I wouldn't expect the methods to spawn any threads. It would just cause the thread that is currently executing the method to wait.

The line says "any of the objects", which implies that the current thread picks one object and waits for it, but that would be non-sensical and also not in line with the name of the method, which implies the current thread needs to wait for every object.

We're making a lot of assumptions. It would be really helpful if we could see more of the assignment. How long is "long"?
 
Filippo Restori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i will send the entire trace, but is very hard to undersand I think.

It's ok if you can't help me XD

A fence is an object that has a collection of objects at its disposal and can be put on hold for any of the objects in the collection to be notified. In particular, the Fence in Java implement the following interface:

The add (Object) method allows you to add an object to the fence object collection.
The awaitO method allows you to wait for any of the objects in the collection to be reported using notifyQ or notifyAll (). Note that if the add (Object) method is called while a wait is in progress, then the argument passed to the add (Object) method is also immediately involved in the wait.
Write the source code of the Fencelmpl class in the test.exam package to implement the Fence interface.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic