Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread problem

 
HK Stojkovic
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Howard Kushner
author
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
HK
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic