Java
application Java main applet Web applet Runnable Thread
CPU Thread 1 Thread 2 Thread 3 CUP Thread 1 Thread 2 Thread 3
,,.
(new) Thread (runnable) start( ) CPU (running) run ( ) blocked CPU sleep( ) I/O wait( ) (dead) stop( )
resume( ) notify( ) notifyall( ) start( ) run( ) yield( ) stop( ) sleep( ) wait( ) suspend( ) stop( ) stop( )
void run() Java. void start() run( ). static void sleep(long millis) throws InterruptedException.
void stop(). (deprecated in JDK 1.2) null void suspend() (deprecated in JDK 1.2). wait( ) void resume() (deprecated in JDK 1.2). notify( )
Java run class Task1 extends Thread{ public void run(){ // // class Task2 implements Runnable{ public void run(){ // // Runnable
Thread // Custom thread class public class CustomThread extends Thread {... public CustomThread(...) {... // Override the run method in Thread public void run() { // Tell system how to run custom thread...... // Client class public class Client {... public somemethod() {... // Create a thread CustomThread thread = new CustomThread(...);... // Start a thread thread.start();...
Example 13.1 Thread Objective: : 100 a. 100 b. 1 100. TestThread Run
Runnable // Custom thread class public class CustomThread implements Runnable {... public CustomThread(...) {... // Implement the run method in Runnable public void run() { // Tell system how to run custom thread...... // Client class public class Client {... public somemethod() {... // Create an instance of CustomThread CustomThread customthread = new CustomThread(...); // Create a thread Thread thread = new Thread(customThread); // Start a thread thread.start();......
Example 13.2 Runnabel Objective: Runnable 13.1 TestRunnable Run Thread printa = new Thread(new PrintChar('a',100)); Thread printb = new Thread(new PrintChar('b',100)); Thread print100 = new Thread(new PrintNum(100)); Thread Runnable Thread
Thread.NORM_PRIORITY. setpriority(int priority). 1 10 Thread int 1 5 10 Thread.MIN_PRIORITY = 1 Thread.MAX_PRIORITY = 5 Thread.NORM_PRIORITY = 10
Java java.lang ThreadGroup getthreadgroup()
ThreadGroup : ThreadGroup g = new ThreadGroup("timer thread group"); Thread : Thread t = new Thread(g, new ThreadClass(), "This thread"); activecount() : System.out.println( The number of + runnable threads in the group + g.activecount());
Example 13.3 Objective: 100 1 PiggyBankWithoutSync Run
Example 13.3, cont. Object Thread 1 PiggyBankWithoutSync -PiggyBank bank -Thread[] thread 1 Object +main AddAPennyThread +run() 100 1 PiggyBank -balance +getbalance +setbalance
Example 13.3, cont. thread 100 g activecount 0 0 100
. 13.3 Step balance thread[i] thread[j] 1 0 newbalance = bank.getbalance() + 1; 2 0 newbalance = bank.getbalance() + 1; 3 1 bank.setbalance(newbalance); 4 1 bank.setbalance(newbalance); 1 thread[i] 2 thread[j] 3 thread[i] 4 thread[j] thread[i]
13.3 thread[i] thread[j] Java synchronized 13.3 13.3 PiggyBankWithSync Run
Applet 12.1, applet CurrentTimeApplet start()
Applet public void start() { while (true) { stillclock.repaint(); try { Thread.sleep(1000); catch(interruptedexception ex) { 1000 while
Applet 1. MyApplet implements Runnable. 2. MyApplet 3. init 4. run public class MyApplet extends JApplet implements Runnable { private Thread timer = null; public void init() { timer = new Thread(this); timer.start();... public void run() {...
Applet public void run() { while (true) { repaint(); try { thread.sleep(1000); waitfornotificationtoresume(); catch (InterruptedException ex) {
Applet private synchronized void waitfornotificationtoresume() throws InterruptedException { while (suspended) wait(); Synchronized
Applet public synchronized void resume() { if (suspended) { suspended = false; notify(); public synchronized void suspend() { suspended = true; Synchronized resume() suspend() suspended
Example 13.4 Applet Objective:. ClockApplet Run Applet Viewer
Timer while 1 Java Java javax.swing.timer Timer ActionEvent ActionEvent Timer
Example 13.5 Runnable StillClock ActionListener JApplet Clock 1 1 ClockPanel 3 1 ClockGroup +run() +suspend() +resume() -jlbltitle -clock jbtresume -jbtsuspend +settitle(string title) +resume() +suspend() +actionperformed(e) -clockpanel1 -clockpanel2 -clockpanel3 -jbtresumeall -jbtsuspendall +jbtresumeall() +jbtsuspendall() +actionperformed(e) +main +init() ClockGroup ClockPanel Clock Run
Thread Runnable Thread Runnable Timer Java
1 Java applet HTML <param> 4 Index Name S&p 500 Current Time 15:54 919.01 4.54 applet 5 S&p 500 NIKKEI Gold&Silver applet
2 Java applet Start Stop Reverse JPanel Fan FanControlPanel FanControlPanel Java applet applet
2 Runnable AdjustmentListener JPanel JPanel ActionListener JApplet Fan FanControlPanel FanShowClass
3 Java applet FanControlPanel applet