Hi,
I am using TimeReminder to repeat a thread several times in a specific start date. Ther thread is emplemented with Semaphore and with a simple execution without time looping it works good! this is the code of the simple execution:
and here is the console result:Code:public class simpleTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i < 5; i++) { Processor processor = new Processor("THREAD-" + i); new Thread(processor).start(); } } }
basically, it allows just 3 threads to be executed at the same time, and I loop for 5 threads. It works good but with the TimeReminder, when I want to add a new date and simple interval, with this code:12:43:39 Complex processing started for THREAD-2.
12:43:39 Complex processing started for THREAD-4.
12:43:39 Complex processing started for THREAD-3.
12:43:40 Processing completed by THREAD-4.
12:43:40 Complex processing started for THREAD-1.
12:43:40 Processing completed by THREAD-2.
12:43:41 Processing completed by THREAD-3.
12:43:44 Processing completed by THREAD-1.
it gives me an error that I was not able to fixCode:import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class SemaphoreTest { // static Date now = new Date(); public static Date endDate; public static Date startDate; public static int numEquipment = 10; DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); { try { startDate = dateFormat.parse("05/26/2011 11:10:12"); endDate = dateFormat.parse("05/28/2011 12:10:12"); } catch (ParseException e) { e.printStackTrace(); } } static class RemindTask extends TimerTask { // init endDate (schedule) @Override public void run() { if (SemaphoreTest.endDate.after(new Date())) { for (int i = 1; i < numEquipment; i++) { Processor processor = new Processor("THREAD-" + i); new Thread(processor).start(); } } else { System.exit(0); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Timer timer; timer = new Timer(); timer.schedule(new RemindTask(), SemaphoreTest.startDate, 20 * 1000); } }
Hope someone can help!Exception in thread "main" java.lang.NullPointerException
at java.util.Timer.schedule(Timer.java:257)
at SemaphoreTest.main(SemaphoreTest.java:49)



Reply With Quote
