import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierTester {
private static final int numOfThreads = 5;
public static void main (String[] args) {
startTasks ();
}
private static void startTasks () {
try {
CyclicBarrier barrier = buildBarrier();
startThreads(barrier);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static CyclicBarrier buildBarrier () {
CyclicBarrier barrier = new CyclicBarrier (numOfThreads, new Runnable () {
public void run () {
Logger.log (LogLevel.LOG, " -- barrier action done");
}
});
return barrier;
}
private static void startThreads (CyclicBarrier barrier) {
Thread[] threads = new Thread[numOfThreads];
for (int i = 0; i < threads.length; i++) {
TaskExecuter executor = new TaskExecuter ();
executor.setName ("CleanupThread-" + i);
executor.setBarrier (barrier);
threads[i] = executor;
threads[i].start ();
}
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join ();
} catch (InterruptedException ignore) {
}
}
}
}
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.BrokenBarrierException;
public class TaskExecuter extends Thread {
private CyclicBarrier barrier ;
public void setBarrier (CyclicBarrier barrier){
this.barrier = barrier;
}
public void run () {
Logger.log (LogLevel.LOG, " --- {0} is started processing....", this.getName ());
try {
doAction ();
} catch (Exception e) {
Logger.log(LogLevel.EXCEPTION,"[{0}]",e);
}
Logger.log (LogLevel.LOG, " --- {0} is finished processing....", this.getName ());
}
private void doAction () {
try {
//do your work
} catch (Exception ex) {
ex.printStackTrace();
} finally {
waitForOthers ();
}
}
private void waitForOthers () {
try {
Logger.log (LogLevel.LOG, "{0} is waiting for the others, currentwaiters count :{1}",
getName (), barrier.getNumberWaiting ());
barrier.await (100000L, TimeUnit.MILLISECONDS);
Logger.log (LogLevel.LOG, "{0} is out of the barrier ", getName ());
} catch (BrokenBarrierException e) {
barrier.reset();
Logger.log (LogLevel.WARNING, " -- resetting the barrier for the exception : {0}", e);
} catch (InterruptedException ex) {
Logger.log (LogLevel.EXCEPTION, " --{0}", ex);
} catch (TimeoutException ex) {
Logger.log (LogLevel.EXCEPTION, " --{0}", ex);
}
}
}
Tuesday, June 15, 2010
update date column in mysql
update `myDb`.`myTable` set myColumn = (DATE_SUB(sysdate(), INTERVAL 1 DAY));
Sunday, May 9, 2010
Visualization of Java Concurrent Package
There is a good job here. It can help us to understand topics easily.
download
Download the jar file and run it via: java -jar /home/serkans/Downloads/concurrentanimated.jar
download
Download the jar file and run it via: java -jar /home/serkans/Downloads/concurrentanimated.jar
Friday, May 7, 2010
Get Desktop Snapshot with Java
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class Screenshooter {
public static void main (String[] args) throws Exception {
Screenshooter screenshooter = new Screenshooter ();
screenshooter.captureScreen ("/tmp/desktopSnapshot.png");
}
public void captureScreen (String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
Rectangle screenRectangle = new Rectangle (screenSize);
Robot robot = new Robot ();
BufferedImage image = robot.createScreenCapture (screenRectangle);
ImageIO.write (image, "png", new File (fileName));
}
}
Subscribe to:
Posts (Atom)