i have been struggling problem 2 hours still couldn't found solution.
actually new java, , not @ familiar java's synchronized. don't know if need use synchronized achieve need.
this structure of package.
---------------------------- |----recorder.java | -------capturescreen.java | -------capturesound.java |----start(); |----stop(); i building screen recorder web app. have made screen capturer , separate class can record audio too. not able make them work concurrently.
i have
capturesound sound = new capturesound(); when start sound recording, waits till stop recorder. here problem is. wish after doing sound.start() program should go down execute other statements of recorder.java, i.e. recorder.java shouldn't wait execution of capturesound(). stops. how make them both run concurrently? can later sound.stop() recorder.java itself.
what synchronized does
the synchronized key word means few things
- the section surrounded
synchronizedcannot have multiple threads running in areas synchronized same object, in other words guaranteed synchronous access ifistruefalsein synchronized section, in current content, unless acted on thread in synchronized section remain value. - it means operations not reordered optimization purposes
- when used method descriptor, having method following sort of style
public synchronized void dowork(object parameter) { /*do stuff*/ }
translates following:
public synchronized void dowork(object parameter) { synchronized(this) { /*do stuff*/ } }
synchronized not magically start threads
you need start threads yourself, can't tell how many times find people thinking because make method or synchronized block magically spins thread without them doing work, way control access critical sections of code.
creating new thread
there few ways this, best way (imo) use class implements runnable , have thread consume it
recorder recordin= new recorder(); scanner scanin= new scanner(); thread threadrecord = new thread(new runnable() { private final recorder target = recordin; @override public void run() { //do stuff in here }
}); thread threadscan = new thread(new runnable() { private final recorder targetsrecorder = recordin; private final scanner target = scanin; @override public void run() { //do stuff here
}
});
threadrecords.start(); threadscan.start();
making sure code works
now need make sure synchronized code works. depending on trying need concerned different things. 1 commonly used example having thread wait on 1 thread, waiting on object handle. example want scan recorder every time movement noticed. also, please remember doing on fly without ide, , assuming these 2 threads in operation acting on objects
thread threadrecord = new thread(new runnable() { private final recorder target = recordin; private final scanner scanr= scanin; @override public void run() { //if movement noticed compared last frame save recordin.startrecording(); while(true){ synchronized(target ){ while(target.hasmore()){ synchronized(scanr) { while(!target.currentimage().equals(scanr.getlatestimage()) { target.recordmore(); scanr.wait(); } } } } } } }
}); thread threadscan = new thread(new runnable() { private final recorder targetsrecorder = recordin; private final scanner target = scanin; @override public void run() { //do stuff here synchronized(target){ target.wait(); while(true) { while(!targetsrecorder.donerecording() && targetsrecorder.currentimage().equals(scanin.getlastimage())) target.wait();
if(targetsrecorder.donerecording()) return; scanin.scancurrentimage(targetsrecorder); } } }
}
});
threadscan.start(); threadrecord.start();
doing event listners
as said doing event listeners, concept same spin new thread in event listner
//inside of listner public void somerecordeventhappened(recordevent event){ new thread(new runnable { private final recordevent eventdate = event; @override public void run() { //do stuff asynchronously
} }).start(); }
Comments
Post a Comment