Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Multithreading, Please help me...

Can some please help to write an application in Java that creates a thread pool of 3 or more threads. Each of the threads should generate a random # between 1 and 50, print out the thread name & the random # & add the random # to a vector. After each thread has added a # to the vector, print out the vector & the sum of the # in the vector.

.
[352 byte] By [Camille83] at [2007-11-11 8:51:27]
# 1 Re: Multithreading, Please help me...
I wont write it for you but the basics are as follows:

public class test implements Runnable{
public void Run(){
(Do tasks)
}
}

then start it with
new thread(new tests()).start

note: run isn't allowed to have parameters passed to it if you want other stuff done first/to pass arguments do it in the constructor
chacham15 at 2007-11-11 22:34:09 >
# 2 Re: Multithreading, Please help me...
what you ask for looks like the producer / consumer pattern. i had implemented it in several programms. but here you can lern it by yourself:
http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html
graviton at 2007-11-11 22:35:14 >