简体   繁体   English

Java中的线程

[英]Threads in java

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); 如果我想为下面指定的2个作业创建线程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. 提前致谢。

A Thread usually takes a java.lang.Runnable as argument and can only take one Runnable at a time. Thread通常使用java.lang.Runnable作为参数,并且一次只能使用一个Runnable 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. Job类实现Runnable接口。

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. 如果您有两个可运行对象,则将需要两个线程,或者自己在一个Thread对象中的另一个可运行对象中管理这两个可运行对象。

See Thread(Runnable r) 参见Thread(Runnable r)

I'll also refer you back to my answer to your previous question and ThreadPoolExecutor. 我还将参考您对上一个问题和ThreadPoolExecutor的回答 ThreadPoolExecutor won't guarantee ordering of jobs presented to it. ThreadPoolExecutor不保证对提供给它的作业进行排序。 If you require that, then you should probably manage the running of these yourself. 如果需要,那么您应该自己管理这些文件的运行。

Be sure to take a look at the new java.util.concurrent classes, which make concurrent programming much easier and less error-prone. 一定要看一看新的java.util.concurrent类,它们使并发编程变得更加容易且不易出错。 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. 这将形成一个包含三个线程的池,然后向该池提供五个要执行的任务。

I highly suggest you take a look at the Java Executor Interfaces . 我强烈建议您看一下Java Executor接口

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. 以下小节详细介绍了执行程序。

final Job1 objJob1 = new Job1 ();
final Job2 objJob2 = new Job2 ();
Thread objHygThread = new Thread(new Runnable(){
    public void run() { objJob1.run(); objJob2.run(); }
});

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();

Looks like you first need to create a class that extends java.lang.Thread so you can have a constructor that takes 2 parameters. 看来您首先需要创建一个extends java.lang.Thread的类,以便您可以使用带有2个参数的构造函数。

You also presumably need to override public void run() in your Thread subclass so it knows what to do with the 2 job objects. 您可能还需要在Thread子类中重写public void run() ,以便它知道如何处理2个作业对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM