Jeb
Jeb

Reputation: 517

how to call a method with multiple threads

I have a method 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. Each and every individualObject contains it's own sleep time.

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

Upvotes: 0

Views: 628

Answers (2)

Gen
Gen

Reputation: 2540

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

}

Upvotes: 1

Conffusion
Conffusion

Reputation: 4485

You have to put your logic in a Runnable and launch it using 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:

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

Upvotes: 1

Related Questions