views:

219

answers:

7
int iThreadCount = 1;
iThreadCount = GHMTreadUtil.getHygThreadCount();
arrHygThread = new Thread[iThreadCount];

for(int iCount=0;iCount<iThreadCount;iCount++)
{
    LogMgr.logDebugInfo("spawning the HYG Thread"+iCount,objDebug);

    Job1 objJob1=new Job1 ();
    Job2 objJob2 =new Job2 ();
    Thread objHygThread = new Thread(objJob1,objJob2);
    arrHygThread[iCount]=objHygThread;
    objHygThread.start();
}

If i want to create the thread for the 2 jobs as specified below Thread objHygThread = new Thread(objJob1,objJob2);

Need i used to import anything? If anyone knows answer for this,then pls let me know.

Thanks in advance.

+5  A: 

A Thread usually takes a java.lang.Runnable as argument and can only take one Runnable at a time. So you would need to start two threads:

Thread objHygThread1 = new Thread(objJob1);
Thread objHygThread2 = new Thread(objJob2);

if the Job class implements the Runnable interface.

Christian Hang
+3  A: 

A thread will take one runnable only. If you have two runnables, you're going to need two threads, or manage those two runnables yourself within one other runnable in a Thread object.

See Thread(Runnable r)

I'll also refer you back to my answer to your previous question and ThreadPoolExecutor. ThreadPoolExecutor won't guarantee ordering of jobs presented to it. If you require that, then you should probably manage the running of these yourself.

Brian Agnew
A: 

Looks like you first need to create a class that extends java.lang.Thread so you can have a constructor that takes 2 parameters.

You also presumably need to override public void run() in your Thread subclass so it knows what to do with the 2 job objects.

QuickRecipesOnSymbianOS
+1  A: 
final Job1 objJob1 = new Job1 ();
final Job2 objJob2 = new Job2 ();
Thread objHygThread = new Thread(new Runnable(){
    public void run() { objJob1.run(); objJob2.run(); }
});
cadrian
+3  A: 

Be sure to take a look at the new java.util.concurrent classes, which make concurrent programming much easier and less error-prone. Here's a relevant example from a U. of Hawaii lecture:

public class Task implements Runnable {
    private String message;
    private int iterations;
    public Task(String s, int n) {
        message = s; iterations = n;
    }
    public void run() {
        for (int i=0; i < iterations; i++) {
            System.out.println(message);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) { }
        }
    }
}

java.util.concurrent.*;
. . .
ExecutorService pool;
pool = Executors.newFixedThreadPool(3);
pool.execute(new Task(“three”,3));
pool.execute(new Task(“two”,2));
pool.execute(new Task(“five”,5));
pool.execute(new Task(“six”,6);
pool.execute(new Task(“one”,1);
pool.shutdown();

This makes a pool of three Threads and then feeds the pool five Tasks to execute.

Jim Ferrans
+1  A: 

Unrelated to the question at hand, but I hope you realize that the assignment in the first line here is a dead store and is completely unnecessary:

int iThreadCount = 1;
iThreadCount = GHMTreadUtil.getHygThreadCount();

Could (and should) just simply be:

int iThreadCount = GHMTreadUtil.getHygThreadCount();
matt b
+2  A: 

I highly suggest you take a look at the Java Executor Interfaces.

It makes sense to separate thread management and creation from the rest of the application. Objects that encapsulate these functions are known as executors. The following subsections describe executors in detail.

Peter D