简体   繁体   English

如何使用多个线程调用方法

[英]how to call a method with multiple threads

I have a method sendMail(list) .我有一个方法sendMail(list) This method will send the mails to the recipients which are there in the list.此方法会将邮件发送给列表中的收件人。

public void sendMail(List<DTO> dto) {


        for(DTO individualObject: dto) {
            
            bulkMailSender.sendSimpleMessage(individualObject.getEmail(),masterDetails.getMailSubject() , content, masterDetails.getMailFrom(), individualObject); 
            try {
                TimeUnit.MINUTES.sleep(Long.parseLong(individualObject.getTimegap().trim()));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }

}

I have this kind of method.我有这种方法。 I want to run this method Thread based, when one thread is executing the mails, I should allow the other thread to access sendMail and send simultaneously together.我想运行这个基于线程的方法,当一个线程正在执行邮件时,我应该允许另一个线程访问sendMail并同时发送。 Each and every individualObject contains it's own sleep time.每个individualObject包含它自己的睡眠时间。

How can I make it worked with the multiple threads.我怎样才能让它与多个线程一起工作。

Let's take an example让我们举个例子

import java.util.concurrent.TimeUnit;

public class SleepClass {
    public static void main(String[] args) {
        SleepClass s= new SleepClass();
        s.m1(10000);
        s.m1(20000);
        
        
    }
    
    public void m1(int time) {
        
        for(int i = 0; i< 3; i++) {
            System.out.println(i);
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }

}

In the above example I have a regular method and it is executing one by one.在上面的例子中,我有一个常规方法,它正在一个一个地执行。 How can make it simultaneous execution如何让它同时执行

You have to put your logic in a Runnable and launch it using new Thread(runnable).start() .您必须将您的逻辑放入 Runnable 并使用 new Thread(runnable).start()启动它。 To pass parameters to each runnable define them as class variables so you can pass them via the constructor and use them in the run method:要将参数传递给每个 runnable 将它们定义为类变量,以便您可以通过构造函数传递它们并在run方法中使用它们:

public class SleepClass {

    public static void main(String[] args) {
        SleepClass s= new SleepClass();
        s.m1(10000);
        s.m1(20000);
    }
    
    public void m1(int time) {
        for(int i = 0; i< 3; i++) {
            new Thread(new Launcher(i,time)).start();
        }
    }

    public class Launcher implements Runnable {
        int i;
        int time;
        public Launcher(int i, int time) {
            this.i=i;
            this.time=time;
        }
        @Override
        public void run() {
            System.out.println(i);
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }                   
        }
        
    }
}

if you need simultaneous execution and each time new thread you can find the solution here如果您需要同时执行并且每次都有新线程,您可以在这里找到解决方案

public class SleepClass {
    public static void main(String[] args) {
        SleepClass s= new SleepClass();
        s.m2(500);
        s.m2(1000);
    }
    
    public void m2(int time) {
        SleepClass s= new SleepClass();
        new Thread(() -> {
            s.m1(time);
        }).start();
    }
    
    public void m1(int time) {
        
        for(int i = 0; i<= 10; i++) {
            System.out.println(i);
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }

}

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

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