multithreading - Running two tasks simultaneously in Java -


i have 2 tasks should run together. first task save data database. , second task of recording video.

currently use thread each task, , run simultaneously.

... thread insertdb = new thread(new runnable() {             @override             public void run() {                 // insert database                 setdatamediavisit(thumbstr);                 insertvisitrecord();             }         });          thread capture = new thread(new runnable() {             @override             public void run() {                 if (getgraph().getstate() == dscapture.preview) {                     getgraph().setcapturefile("data/"+ capturecontroller.getnomr() +"/videos/"+videostr, dsfilterinfo.filterinfoforprofile(new file("profiles/demo_profile800x570_wm8_vbr_100.prx")), dsfilterinfo.donotrender(), true);                     getgraph().record();                 }                  setdata(capturecontroller.getnomr());             }         });          insertdb.start();         capture.start(); ... 

is above code thread safe? want use edt, know edt java swing component. cmiiw

thank you.

thread safe issue, when want use object running in specific thread thread. it's not clear here using share object in 2 thread or not! but, if wanna use share object or want read , write file or specific butter, can use lock object this:

final object lock = new object();  // in thread 1 // todo: process in thread on  synchronized(lock) {     // todo: put result in somewhere thread2 want read }   // in thread 2 synchronized(lock) {     // todo: result place put in thread 1 }  // todo: process on thread 2 on data 

you should remember need put smallest possible synchronized, because if other thread reach synchronized part, wait until thread 1 finish synchronized block , can kill performance of code


Comments