aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread problem" Watch "Thread problem" New topic
Author

Thread problem

HK Stojkovic
Greenhorn

Joined: Dec 19, 2000
Posts: 7
Hi all,
A question about handling multi-threads.
I want to use multithread and to avoid exhausting the resources, only 4 threads are run simultaneously. After any thread is completed, the 5th will then start. And if the another is also completed, the 6th will then start and so on and so on. Therefore, basically only 4 threads are running simultaneously until all the threads are completed.
Any hints on handling such kind of programming or any sample codes?
Thank a lot



HK
Howard Kushner
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
My favorite book on the subject is by Doug Lea, entitled Concurrent Programming in Java, Design Principles and Patterns. It is published by Addision Wesley, but is not for the faint hearted. This is real industrial grade material!


Howard Kushner<br />IBM Certified Enterprise Developer - WebSphere Studio Application Developer V5.0<br />IBM Certified Advanced System Administrator - WebSphere Application Server V5.0<br />IBM Certified Solution Developer - Web Services with WebSphere Studio V5.1<br /><a href="http://www.amazon.com/exec/obidos/tg/detail/-/1931182108/" target="_blank" rel="nofollow">Developing J2EE Applications with WebSphere Studio</a> my Certification Study Guide for IBM Test 287
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
My favorite book on the subject is by Doug Lea, entitled Concurrent Programming in Java, Design Principles and Patterns. It is published by Addision Wesley, but is not for the faint hearted. This is real industrial grade material!
Paul Hyde's "Java Thread Programming" is complete, yet much lighter treatment of the subject than Doug Lea's book.
Liu Fei
Greenhorn

Joined: Oct 31, 2003
Posts: 2
hi~
Thread pool technology may be useful. I've programmed a very simple sample for you. Wish it is usful for you.

/*
Job.java
*/
public class Job {
private int i;

public Job(int i) {
this.i = i;
}

public void doJob() {
System.out.println("Job " + i + " is doing...");
try {
Thread.sleep(Math.round(Math.random() * 10000));
} catch (InterruptedException e) {
}
System.out.println("Job " + i + " is done.");
}
}
/*
Worker.java
*/
public class Worker extends Thread {
private Job job = null;
private WorkerPool pool;

public synchronized void run() {
while (true) {
try {
wait();
if (job != null) {
job.doJob();
pool.returnWorker(this);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Worker(WorkerPool pool) {
this.pool = pool;
}
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
}
/*
WorkerPool.java
*/
import java.util.ArrayList;
public class WorkerPool {
private int num;
private ArrayList threadList = new ArrayList();
public WorkerPool(int n) {
this.num = n;
for (int i = 0; i < num; i++) {
Worker worker = new Worker(this);
worker.start();
threadList.add(worker);
}
}
public WorkerPool() {
this(4);
}
public void releaseWorkers() {
for(int i = 0; i < num; i++) {

}
}

public synchronized void requestWorker(Job job) throws InterruptedException {
if (threadList.isEmpty())
wait();
Worker worker = (Worker) threadList.remove(0);
synchronized (worker) {
worker.setJob(job);
worker.notify();
}
}
public synchronized void returnWorker(Worker worker) {
threadList.add(worker);
notify();
}
}
/*
Main.java
*/
import java.util.ArrayList;
public class Main {
private ArrayList jobList = new ArrayList();
private WorkerPool pool = new WorkerPool();

private void initJobs(int n) {
for (int i = 0; i < n; i++) {
Job job = new Job(i);
jobList.add(job);
}
}

private void processJobs(int n) {
for (int i = 0; i < n; i++) {
try {
Job job = (Job) jobList.get(i);
pool.requestWorker(job);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
}
}
public void run(int n) {
initJobs(n);
processJobs(n);
}

public static void main(String[] args) {
new Main().run(20);
}
}
And the result may be:
Job 0 is doing...
Job 1 is doing...
Job 2 is doing...
Job 3 is doing...
Job 3 is done.
Job 4 is doing...
Job 4 is done.
Job 5 is doing...
Job 0 is done.
Job 6 is doing...
Job 6 is done.
Job 7 is doing...
Job 2 is done.
Job 8 is doing...
Job 8 is done.
Job 9 is doing...
Job 5 is done.
Job 10 is doing...
Job 1 is done.
Job 11 is doing...
Job 9 is done.
Job 12 is doing...
Job 7 is done.
Job 13 is doing...
Job 11 is done.
Job 14 is doing...
Job 10 is done.
Job 15 is doing...
Job 12 is done.
Job 16 is doing...
Job 15 is done.
Job 17 is doing...
Job 14 is done.
Job 18 is doing...
Job 18 is done.
Job 19 is doing...
Job 17 is done.
Job 13 is done.
Job 19 is done.
Job 16 is done.
BTW: This program will not exit since it is too simple to terminate the child threads.
[ November 01, 2003: Message edited by: Liu Fei ]
HK Stojkovic
Greenhorn

Joined: Dec 19, 2000
Posts: 7
Thanks
HK
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread problem