Java get data from thread

Bharat on Java

There are a couple of ways in which threads can be created. i.e. Implementing Runnable Interface and extending Thread Class (Example here). Further we need to implement run method and do our processing inside the run method. This would look like below.

package com.bharatonjava.threads; public class DoSomeTask implements Runnable < public static void main(String[] args) < Thread thread = new Thread(new DoSomeTask()); thread.start(); >/** * Implement this method defined by Runnable Interface */ public void run() < System.out.println("your business logic goes in here."); >>

One thing to note about above snippet is the return type of run() method. It is void. Means our thread can not return any value. What if your thread does some processing and you want to capture that output?

Java’s Concurrency API provides and elegant way to do this. Let us see this with the help of a simple example.

In our example we want to spawn a few threads to get Employee data from a DAO class and capture the data returned by those threads. Further you might want to send this collected data to your client as a response to his request. but this is not a part of our example in favor of simplicity.

1. Example Project Structure

returning-threads

2. Employee Domain Object

package com.bharatonjava.domain; public class Employee implements Comparable < private int id; private String name; private String location; public Employee() < >public Employee(int id, String name, String location) < super(); this.id = id; this.name = name; this.location = location; >public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String name) < this.name = name; >public String getLocation() < return location; >public void setLocation(String location) < this.location = location; >@Override public String toString() < return "Employee [id=" + id + ", name=" + name + ", location=" + location + "]"; >public int compareTo(Employee o) < if(this.getId() < o.getId()) < return -1; >else if(this.getId() > o.getId()) < return 1; >return 0; > >

3. DAO Class

EmployeeDao.java is a simple class that does not speak to database but for simplicity creates a collection of Employee Objects and returns one when requested based on employee id.

package com.bharatonjava.dao; import java.util.HashMap; import java.util.Map; import com.bharatonjava.domain.Employee; public class EmployeeDao < private static Mapemps; static < emps = new HashMap(); emps.put(1001, new Employee(1001, "Bharat", "Mumbai")); emps.put(1002, new Employee(1002, "Jayant", "Pune")); emps.put(1003, new Employee(1003, "Ravi", "Banglore")); emps.put(1004, new Employee(1004, "Kapil", "Delhi")); emps.put(1005, new Employee(1005, "Gaurav", "Pune")); > public static Employee getEmployeeById(int empId) < return emps.get(empId); >>

4. Task to be Done

TaskAsCallable.java should contain the task that we want our thread to do. Like in our case we just want to get an Employee instance form DAO using the employee id, but in real world this thread will do much more than this.

package com.bharatonjava.threads; import java.util.concurrent.Callable; import com.bharatonjava.dao.EmployeeDao; import com.bharatonjava.domain.Employee; public class TaskAsCallable implements Callable < private int empId; public TaskAsCallable(int empId) < this.empId = empId; >public Employee call() throws Exception < /* this is where your business logic goes */ return EmployeeDao.getEmployeeById(this.empId); >>

Please note that our class implements Callable interface. This interface has one method named call(). We need to implement this method with the task that we want our thread to perform.

Читайте также:  Css style break all

5. Main Class

In our main Class we create a thread pool of 5 threads using Executors.newFixedThreadPool(5) and then create a collection of Callable tasks.

We receive responses of each thread in the form of a Future object instance. Now to our executor we pass our collection of callables and by calling executorService.invokeAll(lst); method. This method blocks till all the threads complete their execution either gracefully or by throwing an awful Exception. The response of invokeAll() method is collected in a collection of Future instances.

Now we can iterate over this collection to get responses from each thread.

package com.bharatonjava.threads; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import com.bharatonjava.domain.Employee; public class MainClass < public static void main(String[] args) throws InterruptedException, ExecutionException < ExecutorService executorService = Executors.newFixedThreadPool(5); List> lst = new ArrayList>(); lst.add(new TaskAsCallable(1001)); lst.add(new TaskAsCallable(1002)); lst.add(new TaskAsCallable(1003)); lst.add(new TaskAsCallable(1004)); lst.add(new TaskAsCallable(1005)); // returns a list of Futures holding their status and results when all complete List> tasks = executorService.invokeAll(lst); System.out.println(tasks.size() +" Responses recieved.\n"); for(Future task : tasks) < System.out.println(task.get().toString()); >/* shutdown your thread pool, else your application will keep running */ executorService.shutdown(); > >

6. Output

thread-reponse

Here is the output that i got on my console.

Источник

Оцените статью