thread from java.util.concurrent



我有使用线程的代码

Thread thread = new Thread() {
                    public void run() {
                        doSomething();
                    }
                };

我想像这个线程一样使用遗嘱执行人,所以我该怎么做?在java.util.concurrent中使用类似的方法吗?

ExecutorService executorService = Executors.newCachedThreadPool();
Runnable runnable = new Runnable() {
                    public void run() {
                        //my code doing something
                    }
                };

我也有:

List<Runnable> threadsList = new ArrayList<Runnable>();

我有方法:

boolean isAllowedCreateNewThread(Runnable taskToRun){
for(int i = 0; i < threadsList.size(); i++){
        Runnable th = threadsList.get(i);
        if(th.isAlive())doSomething(); //isAlive doesn't work since I have runnable instead of Thread
boolean isAllowed = true ;//I have here some bool function but it doesn't matter
    if(isAllowed){
        threadsList.add(taskToRun);
        //service.submit(taskToRun); 
        executorService.execute(taskToRun);
    }
return isAllowed; 
}

如果我有 List<Thread> threadsList = new ArrayList<Thread>();,不要使用执行方服务,并且可以在无线电话中进行更改以将其工作。所以我认为我也必须改变,但是如何?什么是关键字安装的线程?可运行?或者是其他东西?我也有:

for(int i = 0; i < threadsList.size(); i++){
        Thread th = threadsList.get(i);
        if(th.isAlive())doSomething(); myThreadName.start();

我必须将isAlive()更改为java.util.concurrentmyThreadName.start();中类似的东西因此,通常我希望使用线程类更改此代码,以使用Java.util.concurrent的线程

来代码

您可以用以下代码段替换代码。

  1. 创建一个可运行的类。

    class MyRunable implements Runnable{
        public void run() {
            doSomething();
        }
        private void doSomething() {
            System.out.println("Thread Executing is " + Thread.currentThread().getId());
        }
    }
    
  2. 创建具有所需线程数量的执行器服务。这创建了所需数量的线程的池。

    int numberOfThreads  = 4;  // number of threads you want to create
    ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
    
  3. 为池中存在的每个线程创建可运行的可运行,并将其分配给您的执行人。

    for(int i=0;i<numberOfThreads;i++){
        Runnable myRunable = new MyRunable();
        service.submit(myRunable);
    }
    service.shutdown(); // this is a must as not given ,it halts the termination of the program
    

    样本输出

    Thread Executing is 9
    Thread Executing is 10
    Thread Executing is 8
    Thread Executing is 11
    

,整个代码看起来像:

class MyRunable implements Runnable {
    public void run() {
        doSomething();
    }
    private void doSomething() {
        System.out.println("Thread Executing is " + Thread.currentThread().getId());
    }
    public static void main(String[] args) {
        int numberOfThreads  = 4;  // number of threads you want to create
        ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
        for(int i=0;i<numberOfThreads;i++){
            Runnable myRunable = new MyRunable();
            service.submit(myRunable);
        }
        service.shutdown();
    }
}

您可以使用执行者 java.util.concurren t。
执行者旨在创建和管理线程,因此您不必直接执行此操作。

在您的示例中是:

ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(() -> doSomething());


执行者有多种类型,您必须决定考虑使用哪种调用上下文。
例如,如果您只想有一个线程,则可以使用:

Executors.newSingleThreadExecutor();

固定的线程数,即10:

Executors.newFixedThreadPool(10);

或根据需要创建新线程:

Executors.newCachedThreadPool();


有关执行者的更多信息,您可以在此处阅读。
希望它会有所帮助。

相关内容

最新更新