• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to let 10 thread do the same thing simultaneously ?

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to let 10 threads read a file at the same time ? any snippet ? Just want to see how to handle the thread part, I know how to use I/O to read a file.

thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, put the code in a Runnable object. Create n instances and start them up in threads.

The Sun tutorial on Concurrency and threads is pretty good.

If you have 100 things to do but only want 10 to run at a time, see the doc on the Executor interfaces. If you need to know when they're all done, look for thread.join().

Is that enough to get you started?
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. A more intereting question -- How to let these thread run for an hour ??
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run for an hour doing what? Your questions don't make much sense. Perhaps explaining what it is you hope to accomplish in the "big picture" might help us understand?
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
Run for an hour doing what? Your questions don't make much sense. Perhaps explaining what it is you hope to accomplish in the "big picture" might help us understand?




There is no "big picture". I just want to know how to make several threads simultaneously run for a certain amount of time. Now I know how to run multiple threads simultaneously, but don't know how to "control" the time length. That's my whole question.
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Thread that runs will only run for the time required to perform the task you assign to it. In other words, lets say you create a thread thats going to read a file (or count to 100 5 billion times, whatever). Based on the processor availability and speed, hard drive speed, ram, etc on the machine its running on, however long that task you have coded takes to run, thats how long your thread will execute.

If you need it (for some reason which I cannot even imagine the purpose of, but hey, every requirement is its own, right?) to execute for exactly 1 hour, then catch the start time at the beginning of the thread and catch it after your tasks finish, then use the sleep method to rest the thread for the difference in time.

Of course, that wont help you in the event you task takes more than an hour. In this case, you'll need an external object to monitor the thread and kill it if the time exceed your timeline.

Again.....I cannot even begin to imagine why you would need to have any control whatsoever implemented such that a thread runs for a period of time. Perhaps your question is simply one of wondering, in which case now you know
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, let's put it this way -- how can I let the threads repeatedly perform a task for 100 times ??
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same way you make any thread of execution repeat a task 100 times.

 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, to make a thread run for an hour.



Or you could make a thread stay alive for an hour without actually executing meaningless code.



The thing is, none of this is the kind of thing you should ever need to do hence why we are having trouble fathoming just exactly what it is you want to do.
[ August 23, 2006: Message edited by: Ken Blair ]
 
Henry Wong
author
Sheriff
Posts: 22514
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe Raj is thinking of repeatedly running a particular task, in the span of an hour... To do that, we can make a slight modification to Ken's code...



Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!