- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Herb Schildt s Java Prog ramming Cookbook in Java
Herb Schildt s Java Prog ramming Cookbook Data Matrix 2d Barcode Printer In Java Using Barcode encoder for Java Control to generate, create ECC200 image in Java applications. Scanning DataMatrix In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. // A simple implementation of a "reminder" class // An object of this class starts a daemon thread // that waits until the specified time It then // displays a message class Reminder implements Runnable { // Time and date to display reminder message Calendar reminderTime; // Message to display String message; // message to display // Use this constructor to display a message after // a specified number of seconds have elapsed // This value is then added to the current time // to compute the desired reminder time // // In practice, you might want to change the // delay to minutes rather than seconds, but // seconds make testing easier Reminder(String msg, int delay) { message = msg; // Get the current time and date reminderTime = CalendargetInstance(); // Add the delay to the time and date reminderTimeadd(CalendarSECOND, delay); Systemoutprintf("Reminder set for %tD %1$tr\n", reminderTime); // Create the reminder thread Thread dThrd = new Thread(this); // Set to daemon dThrdsetDaemon(true); // Start execution dThrdstart(); } // Notify at the specified time and date Reminder(String msg, Calendar cal) { message = msg; // Use the specified time and date as the // reminder time reminderTime = cal; Systemoutprintf("Reminder set for %tD %1$tr\n", reminderTime); // Create the reminder Thread dThrd = new Thread(this); Print Barcode In Java Using Barcode creation for Java Control to generate, create bar code image in Java applications. Bar Code Decoder In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. 7: Drawing Data Matrix 2d Barcode In Visual C#.NET Using Barcode maker for VS .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications. ECC200 Generation In VS .NET Using Barcode printer for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications. Multithreading
Making Data Matrix In .NET Using Barcode creation for Visual Studio .NET Control to generate, create DataMatrix image in Visual Studio .NET applications. Data Matrix Encoder In Visual Basic .NET Using Barcode creation for .NET framework Control to generate, create ECC200 image in Visual Studio .NET applications. // Set to daemon dThrdsetDaemon(true); // Start execution dThrdstart(); } // Run the reminder public void run() { try { for(;;) { // Get the current time and date Calendar curTime = CalendargetInstance(); // See if it's time for the reminder if(curTimecompareTo(reminderTime) >= 0) { Systemoutprintln("\n" + message + "\n"); break; // let the thread end } Threadsleep(1000); } } catch(InterruptedException exc) { Systemoutprintln("Reminder interrupted"); } } } class ReminderDemo { public static void main(String args[]) { // Get a reminder 2 seconds from now Reminder mt = new Reminder("Call Harry", 2); // Get a reminder on April 5, 2007 at 2:30 pm Reminder mt2 = new Reminder("Meet with Bill", new GregorianCalendar(2007, 3, 5, 14, 30)); // Keep the main thread alive for 20 seconds for(int i=0; i < 20; i++) { try { Threadsleep(1000); } catch(InterruptedException exc) { Systemoutprintln("Main thread interrupted"); } Systemoutprint(""); } Systemoutprintln("\nMain thread ending"); } } UCC-128 Generator In Java Using Barcode generator for Java Control to generate, create EAN128 image in Java applications. Generate UPC-A Supplement 5 In Java Using Barcode maker for Java Control to generate, create GTIN - 12 image in Java applications. Herb Schildt s Java Prog ramming Cookbook
Printing DataMatrix In Java Using Barcode generator for Java Control to generate, create Data Matrix 2d barcode image in Java applications. Encode Code39 In Java Using Barcode generator for Java Control to generate, create USS Code 39 image in Java applications. The output is shown here: Identcode Generation In Java Using Barcode creation for Java Control to generate, create Identcode image in Java applications. Draw EAN / UCC - 14 In Visual Studio .NET Using Barcode encoder for Visual Studio .NET Control to generate, create GS1-128 image in .NET framework applications. Reminder set for 04/05/07 02:29:56 PM Reminder set for 04/05/07 02:30:00 PM Call Harry Meet with Bill Main thread ending Bar Code Creation In None Using Barcode creation for Microsoft Word Control to generate, create barcode image in Word applications. Generating Bar Code In Objective-C Using Barcode maker for iPhone Control to generate, create barcode image in iPhone applications. Here are a few things that you might want to try To prove the benefit of using a daemon thread to execute the reminder, try commenting out the call to setDaemon( ) in both of Reminder s constructors as shown here: Decoding EAN / UCC - 13 In Visual Basic .NET Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications. Code-128 Creation In Visual Basic .NET Using Barcode creation for VS .NET Control to generate, create USS Code 128 image in VS .NET applications. // dThrdsetDaemon(true); USS Code 128 Scanner In .NET Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET applications. Recognize Data Matrix ECC200 In Visual Basic .NET Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications. Because the thread is no longer marked as daemon, it remains a user thread and the application will not end until the reminder thread has finished Thus, the application hangs until the future time is reached For ease of testing and experimentation, the delay period used by the first Reminder constructor is assumed to be seconds However, for real-world use, this delay is probably better specified in terms of minutes You might want to try this change One optimization that might be applicable in some cases is to simply have the run( ) method sleep for a period of time equal to the difference between the time at which the Reminder was started and the specified future time With this approach, there is no need to keep checking the time However, this approach will not work in situations in which the reminder is to be displayed based on the system time, which might change for example, if the user changes time zones or if daylight saving time comes into effect Finally, you can try having the reminder display a pop-up window that contains the message rather than using a console-based approach This is easily done using Swing s JFrame and JLabel classes JFrame (which is a top-level Swing container) creates the standard window and JLabel displays the message To try this, substitute the following version of run( ) in the preceding program: // Display the reminder in a pop-up window public void run() { try { for(;;) { // Get the current time and date Calendar curTime = CalendargetInstance(); // See if it's time for the reminder if(curTimecompareTo(reminderTime) >= 0) { // Create GUI on the event-dispatching thread // as recommended by Sun 7:
|
|