Danh mục

giáo trình Java By Example phần 9

Số trang: 56      Loại file: pdf      Dung lượng: 139.93 KB      Lượt xem: 15      Lượt tải: 0    
10.10.2023

Phí tải xuống: 31,000 VND Tải xuống file đầy đủ (56 trang) 0
Xem trước 6 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu giáo trình java by example phần 9, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 9 thread.sleep(100); } catch (InterruptedException e) { } } } public void paint(Graphics g) { g.drawString(displayStr, 50, 130); }}Tell Java that the applet uses the classes in the awt package.Tell Java that the applet uses the classes in the applet package.Derive ThreadApplet from Applet and implement Runnable.Declare the classs data fields, including a Thread object.Override the start() method.Create and set the applets display font.Initialize data fields.Create and start the thread.Override the stop() method.Stop the thread.Implement the run() methodLoop one thousand times.Increment the counter.Create the display string from the counter.Tell Java to repaint the applet.Suspend the thread for one hundred milliseconds.Override the paint() method. http://www.ngohaianh.info Draw the display string.There are a couple of interesting things in ThreadApplet of which you should be aware. First, notice thatin run(), the thread loops one thousand times, after which the while loop ends. When the whileloop ends, so does the run() method. This means that when you run ThreadApplet, if you let it countall the way to one thousand, the thread ends on its own. However, what if you switch to a different Webpage before ThreadApplet has counted all the way to one thousand? Then, Java calls the appletsstop() method, which ends the thread by calling the threads stop() method.The next point of interest is whats going on inside run(). At the beginning of the loop, the programincrements the counter, converts the counters value to a string, and then repaints the applet so that thenew count value appears in the window. That code should be as clear as glass to you by now. But whatsall that malarkey after the call to repaint()? Thats where the thread not only times the animation, butalso relinquishes the computer so that other threads get a chance to run. Simply, the call to the threadssleep() method suspends the thread for the number of milliseconds given as its single argument. Inthis case, the sleep time is 100 milliseconds, or one tenth of a second. If you want the animation to runfaster, change the 100 to a smaller value. To count slower, change the 100 to a larger value. CAUTION Its important that your threads not dominate the computers processor for longer than necessary. This is because other threads and processes are almost certainly in competition for the processor at the same time. If your thread will be running for a while, you should call the sleep() or yield() methods in order to give other processes a chance to run. This is more important on some systems than on others, but since you cant know for sure which system your applet will be running on, be a considerate thread programmer.Notice that the call to sleep() is enclosed in a try block and followed by a catch block thatswatching for InterruptedException exceptions. You have to catch this exception because thesleep() method throws it. If you fail to catch the exception, your program will not compile.Deriving a Class from ThreadThe second way to create a thread is to derive a new class from Thread. Then, in your applets class,you create and start a thread object of your thread class. This leaves you with two processes goingsimultaneously, the applet and the thread object created in the class. By giving the thread class access todata and methods in the applet, the thread can easily communicate with the applet in order to performwhatever tasks it was written for.Example: Creating a Thread ClassSuppose that you want to write the same sort of applet as that shown in Listing 31.3, but now you want aseparate thread to control the counting process. Listing 31.4 shows how you might write the new classfor the thread. (Dont try to compile this code yet. Youll use it in the next example in this chapter.) http://www.ngohaianh.infoListing 31.4 MyThread.java: A Class Derived from Thread.public class MyThread extends Thread{ ThreadApplet2 applet; int count; MyThread(ThreadApplet2 applet) { this.applet = applet; } public void run() { count = 0; while (count < 1000) { ++count; applet.displayStr = String.valueOf(count); applet.repaint(); try { http://www.ngohaianh.info sleep(100); } catch (InterruptedException e) { } } } } Derive the MyThread class from Thread. Declare the classs data fields, including a Thread object. Declare the classs constructor. Store the constructors single parameter. Override the run() method Loop one thousand times. Increment the counter. Create the display string from the counter. Tell Java to repaint the applet. Suspend the thread for one hundred milliseconds.The first thing to notice in this thread class is that its constructor takes as a single argument a reference toa ThreadApplet2 object, which is the applet from which youll be running this thread. The threadneeds this reference so that it can communicate with the applet.Next, look at run(). The thread still counts from zero to one thousand, b ...

Tài liệu được xem nhiều: