java - Whether using Threads join correctly or not -


i want execute task take 1000 ms , if exceeds , dont want continue task , have used join .

please tell me , guide me if correct or not

import java.util.list;  public class mainthread {      public static void main(string args[]) throws interruptedexception {         thread mainthread = thread.currentthread();         childthread child = new childthread();         thread childthread = new thread(child);         childthread.start();          mainthread.join(1000);         list list = child.getdata();          if(list.size()<0)          {             system.out.println("no data found");         }     } } 

childthread

import java.util.arraylist; import java.util.list;  public class childthread implements runnable  {      list list = new arraylist();      public list getdata() {         return list;     }      public void run() {         // list data feteched database used static data         list.add("one");         list.add("one2");         list.add("one3");      } } 

nope. incorrect. not need mainthread @ all, should call childthread.join(1000) instead.

but there problem approach - mean child thread anyhow continue running. therefore should call childthread.interrupt() after join:

childthread.join(1000); childthread.interrupt(); 

and in child thread periodically in childthread perform that:

if (interrupted()) {    return; } 

and handle interruptedexception needed - around wait() methods have.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -