i building project watches change in file, when file changes takes last line of file id , uploads id db once id has been added, counter begins time x. below 4 classes work individually im confused on how use threads to:
- when each process complete start next process.
- when whole process complete restart process again , wait until file changed.
any great, , apologises if coding poor, im new this!!
thanks jonny
the file watcher class.
import static java.nio.file.standardwatcheventkinds.entry_modify; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.nio.file.filesystems; import java.nio.file.path; import java.nio.file.paths; import java.nio.file.watchevent; import java.nio.file.watchkey; import java.nio.file.watchservice; public class watcherclock implements runnable{ static string clkid; static boolean done = false; public static void main(string[] args) throws ioexception, interruptedexception { checkfile(); if(!done){ } system.out.println(clkid); } public static string dancerid(){ return clkid; } public static void checkfile() { try { watchservice watcher = filesystems.getdefault().newwatchservice(); path dir = paths.get("/home/jonathan/desktop/"); dir.register(watcher, entry_modify); system.out.println("watch service registered dir: " + dir.getfilename()); while (!done) { watchkey key; try { key = watcher.take(); } catch (interruptedexception ex) { return; } (watchevent<?> event : key.pollevents()) { watchevent.kind<?> kind = event.kind(); @suppresswarnings("unchecked") watchevent<path> ev = (watchevent<path>) event; path filename = ev.context(); system.out.println(kind.name() + ": " + filename); if (kind == entry_modify && filename.tostring().equals("example.txt")) { system.out.println("my source file has changed!!!"); string scurrentline = null; try (bufferedreader br = new bufferedreader(new filereader("/home/jonathan/desktop/example.txt"))) { while ((scurrentline = br.readline()) != null) { system.out.println(scurrentline); clkid = scurrentline; system.out.println(clkid); } } catch (ioexception e) { e.printstacktrace(); } file inputfile = new file("/home/jonathan/desktop/example.txt"); // file file tempfile = new file("mytempfile.txt");// temp file bufferedreader reader = new bufferedreader(new filereader(inputfile)); bufferedwriter writer = new bufferedwriter(new filewriter(tempfile)); string currentline; while((currentline = reader.readline()) != null) { currentline=(""); writer.write(currentline); } writer.close(); reader.close(); boolean successful = tempfile.renameto(inputfile); system.out.println(successful); done = true; } } boolean valid = key.reset(); if (!valid) { break; } } } catch (ioexception ex) { system.err.println(ex); } } @override public void run() { // todo auto-generated method stub checkfile(); } } and counter class timer.
import java.sql.timestamp; import java.util.timer; import java.util.timertask; public class counter implements runnable{ private static timertask mytask = null; public static void main(string[] args) { thread count = new thread(new counter()); count.start(); } @override public void run() { // todo auto-generated method stub } public static void counterstart() { timer timer = new timer("my timer", false); int count = 10; system.out.println("lights on"); mytask = new mytimertask(count, new runnable() { public void run() { system.exit(0); } }); long delay = 1000l; timer.scheduleatfixedrate(mytask, delay, delay); } } class mytimertask extends timertask { private int count; private runnable dowhendone; public mytimertask(int count, runnable dowhendone) { this.count = count; this.dowhendone = dowhendone; } @override public void run() { count--; system.out.println("count is: " + count); if (count == 0) { gettimestamp(); system.out.println("lights off"); cancel(); dowhendone.run(); } } private static void gettimestamp() { java.util.date date= new java.util.date(); system.out.println(new timestamp(date.gettime())); } } the db connection class
import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.util.calendar; import com.mysql.jdbc.preparedstatement; import com.mysql.jdbc.statementimpl; public class dbconn implements runnable { public static void connection() { try { class.forname("com.mysql.jdbc.driver"); system.out.println("worked"); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } public void connectiontomysql() { watcherclock id = new watcherclock(); string host ="jdbc:mysql://localhost/test"; string username ="root"; string password ="password"; string dancerid = watcherclock.clkid; //system.out.println(dancerid); try { connection conn = drivermanager.getconnection(host, username, password); system.out.println("connected:"); // mysql insert statement string query = " insert dancers (id)" + " values (?)"; // create mysql insert preparedstatement preparedstatement preparedstmt = (preparedstatement) conn.preparestatement(query); preparedstmt.setstring (1, dancerid); // execute preparedstatement preparedstmt.execute(); conn.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); system.err.println("got exception!"); system.err.println(e.getmessage()); } } public static void main(string args []){ thread thdb = new thread(new dbconn()); thdb.start(); //connection(); } @override public void run() { // todo auto-generated method stub connectiontomysql(); } } and main class
public class main implements runnable{ watcherclock wc = new watcherclock(); counter c = new counter(); public static void main(string args[]){ thread th1 = new thread(new watcherclock()); thread th3 = new thread(new counter()); try { th1.start(); th1.join(); th3.start(); th3.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public void run() { // todo auto-generated method stub } }
not answer but, makes no sense:
th1.start(); th1.join(); it doesn't make sense because main() thread nothing @ entire time th1 thread running.
if don't want else while th1 doing work, why bother create thread? why not work in main() thread?
th1.run(); the entire point of having threads different threads can different things at same time.
th1.start(); dosomethingelsewhileth1threadisrunning(); th1.join();
Comments
Post a Comment