-2

My data size is huge so I thought of dividing it into chunks and using threads to process it asynchronously. To make it simple let's say I have a list and associate each entry with a thread, so the number of threads is equal to the number of elements. Since I am new to threads in Java so I am not sure how the threads run asynchronously. Here is a simplified code for better understanding.

class ThreadRunner extends Thread { 
  String threadName;
  String element;
  public MyThread (String threadName, String element) { 
    this.threadName = threadName;
    this.element = element;
  }

  public void run() { 
    System.out.println("Run: "+ threadName); 
    // some processing on the item
  } 
}


 class TestThread {
  public static void main (String arg[]) {
    List<String> mainList = new ArrayList<>();


    for (int x=0; x< mainList.size(); x++)
    {
        MyThread temp= new MyThread("Thread #" + x+1);
        temp.start();
        System.out.println("Started Thread:" + x+1);
    }
}

Does this code execute the threads in an asynchronous manner?

Savior
  • 3,225
  • 4
  • 24
  • 48
  • Why do you think it does? Why do you think it doesn't? Have you gathered any evidence one way or another? – Savior Apr 15 '19 at 16:54
  • i've tried to run but the threads are running synchronously. For example, if we alter the above code so that each thread handles 2 elements. Then the second threads runs after the first thread completes. So is there any fault in my logic? – Raafay Alam Apr 15 '19 at 17:02
  • Maybe add way more elements. – Savior Apr 15 '19 at 17:04
  • at the moment I have about 4000 elements in each thread. – Raafay Alam Apr 15 '19 at 17:05
  • And you don't see any interleaving of print messages (you should have a log somewhere in a loop that processes the elements)? – Savior Apr 15 '19 at 17:07
  • I have a log which shows which thread is running and processing at the moment. So there is no interleaving. Only until one thread completely finishes, then a new one start (with log showing its name). I have also tried to put a sleep in between to check if interleaving pops in at some point. – Raafay Alam Apr 15 '19 at 17:10
  • You'd have to provide a [mcve]. – Savior Apr 15 '19 at 17:12
  • You'll find the important aspect of your example code is the `println` - see https://stackoverflow.com/a/9459886/2711811. –  Apr 15 '19 at 17:16
  • what do you mean by "asynchronous manner" when talking about threads? Do you mean "parallel manner"? – Alexei Kaigorodov Apr 16 '19 at 07:15
  • by asynchronous I mean that executing by interleaving between each other – Raafay Alam Apr 16 '19 at 12:02

1 Answers1

0

Instead of spawning threads yourself, use an ExecutorService and submit work to it in the form of Runnables.

Each Runnable task should process enough work to justify the overhead of spawning threads but not so much work that you underutilize the other cores. In other words, you want to properly load balance the work across your cores. One way to do this is to divide the elements evenly across the tasks so that each task processes roughly num_threads / mainList.size()elements and you submit num_thread tasks to the ExecutorService.

Eric
  • 906
  • 7
  • 13