java - How to implement a list of jobs which can be executed one after another or stopped at any time? -


i developing swing application collects jobs (tasks) in list , executes them 1 after another. have 2 buttons: "start" , "stop". "start" should start execution , while jobs worked off should possible click "stop" stop execution.

i think have use multi-threading able interrupt execution @ time. not versed multi-threading , think missing correct approaches.

so far have interface jobs:

public interface job {      void start();      void interrupt();  } 

this way it's possible implementing classes extend thread. if start execution of list, start return , next job started although last job has not finished. if wait execution .join() not able interrupt job.

at moment jobs worked off (jobs has type list<job>):

public void startwork() {     while (!jobs.isempty()) {         jobs.get(0).start();         jobs.remove(0);     } } 

do have idea problem? stuck.

update

thank answers!

i able start jobs now. jobmanager looks this:

public final class jobmanager {      /**      * list of jobs.      */     private list<job> jobs = new arraylist<job>();      /**      * worker thread.      */     private executorservice worker = executors.newsinglethreadexecutor();      /**      * works off job list.      */     public void startwork() {         worker.execute(new runnable() {              @override             public void run() {                 while (!jobs.isempty()) {                     jobs.get(0).start();                     jobs.remove(0);                 }             }          });     }      /**      * stops work off.      */     public void stopwork() {         worker.shutdownnow();     }  } 

but there problem shutdownnow(). inside job have check periodically if thread interrupted. possible kill thread? think periodically checks big overhead , have make sure job checks interrupts.

if understood correctly, want run jobs 1 one - in other words dont need job thread/runnable.

rather , process executes jobs should in thread, swing application can start / stop when required.

if above assumption correct ..

define job

interface job{     void start();     boolean iscompleted(); } 

and actual job

class jobinstance implements job {  string name; boolean iscompleted;  public jobinstance(string name) {     this.name  = name; }   /**  * @return iscompleted  */ public boolean iscompleted() {     return iscompleted; }   @override public void start() {      system.out.println("job "+this.name+" started");     try {         thread.sleep(10000);         system.out.println("job "+this.name+" completed");         iscompleted = true;     } catch (interruptedexception e) {         // todo auto-generated catch block         system.out.println("interuppted.. mark me finish / dont remove list");     }   } 

}

if no interrup fired , job work in normal way , mark completed. make class executing job runnable

public class process implements runnable{  list<job> joblists;  public process(list<job> lists) {     this.joblists = lists; }  @override public void run() {     system.out.println(thread.currentthread().getname());     while(!joblists.isempty()){         job job = joblists.get(0);         job.start();         if (job.iscompleted()){             joblists.remove(0);         }else{             system.out.println("interrup issuesed");             return;         }      } }    

}

you can verify using following...

jobinstance job1 = new jobinstance("a");     jobinstance job2 = new jobinstance("b");     arraylist<job> joblists = new arraylist<>();     joblists.add(job1);     joblists.add(job2);     process process = new process(joblists);     system.out.println(thread.currentthread().getname());     thread thread = new thread(process);     thread.start();      try {         thread.sleep(15000);     } catch (interruptedexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     thread.interrupt(); 

let me know if need info..

note** , thought demonstrate using basic thread , there lot of ways achive same using executors , so.

update

if task/process not blocking , issuing interrupt not stop thread. check thread.interrupt doc/spec more details.

if planning stop non blocking task/ job please find follow sample using executorservice

job manager - per spec starts 1 thread.

public final class jobmanager {   public jobmanager(list<runnable> jobs){     this.jobs = jobs; } /**  * list of jobs.  */ private  list<runnable> jobs = new arraylist<runnable>();  /**  * worker thread.  */ private final executorservice worker = executors.newsinglethreadexecutor();  /**  * works off job list.  */ public void startwork() {      (iterator<runnable> iterator = jobs.iterator(); iterator.hasnext();) {         runnable task =  iterator.next();         worker.execute(task);     } }  /**  * stops work off.  */ public void stopwork() {     worker.shutdownnow();     jobs.clear(); } } 

your task... implement process ever in run method.

class task implements runnable{ string name;   public task(string name) {     super();     this.name = name; }   @override public void run() {     system.out.println("job "+this.name+" started");     long k=1;     for(int i=0;i<=integer.max_value/1.5;i++){         k = k +k;     }      system.out.println("job  "+this.name+ "completed"); }   } 

test

public static void main(string args[]){ task job1 = new task("a"); task job2 = new task("b"); arraylist<runnable> joblists = new arraylist<>(); joblists.add(job1); joblists.add(job2); jobmanager manager = new jobmanager(joblists); manager.startwork(); try {     thread.sleep(150); } catch (interruptedexception e) {     e.printstacktrace(); } manager.stopwork(); system.out.println("issued stop"); 

}


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -