Thread boonritfeu ac th Creating and Running Threads
Thread boonrit@feu. ac. th
Creating and Running Threads in Java n n n ����� instance ��� Thread object ����� start() method ����� thread ����� Thread my. Thread = new Thread(); my. Thread. start();
���� Thread ���� n n ������ Thread class ����� override run() method public class My. Thread extends Thread { } public void run() { System. out. println("Do something cool here. "); } Thread my. Thread = new My. Thread(); my. Thread. start();
���� Thread ����������� Thread class n n n public class Number. Thread extends Thread { int num; public Number. Thread(int n) { num = n; } public void run() { for (int k=0; k < 10; k++) { System. out. print(num); } // for } // run() } // Number. Thread class
���� Thread ����������� Thread class n n n public class Numbers { public static void main(String args[]) { // 5 threads Number. Thread number 1, number 2, number 3, number 4, number 5; // Create and start each thread number 1 = new Number. Thread(1); number 1. start(); number 2 = new Number. Thread(2); number 2. start(); number 3 = new Number. Thread(3); number 3. start(); number 4 = new Number. Thread(4); number 4. start(); number 5 = new Number. Thread(5); number 5. start(); } // main() } // Numbers class ������� 1111122222333334444455555
���� Thread ����������� Thread class n 11111111111111111111111111111111111111111111111111111111111111111111122222222222222222222222222222222222222222222222222222222222222222222233333333333333333333333333333333333444444444444444444444444444444444445555555555555555555555555555555222222233333333333333333333333333333333344444444444444455555555555555555555555555544444444444444444
���� Thread ����� Implementing the Runnable Interface n public class My. Class extends Some. Other. Class implements Runnable { n n n n } public My. Class() { Thread thread = new Thread(this); thread. start(); } public void run() { System. out. println("Do something cool here. "); }
���� Thread ����� Implementing the Runnable Interface
���� Thread ����� Implementing the Runnable Interface n n n n n public class Number. Printer implements Runnable { int num; public Number. Printer(int n) { num = n; } public void run() { for (int k=0; k < 10; k++) System. out. print(num); } // run() } // Number. Printer class
���� Thread ����� Implementing the Runnable Interface n n n public class Numbers { public static void main(String args[]) { Thread number 1, number 2, number 3, number 4, number 5; // Create and start each thread number 1 = new Thread(new Number. Printer(1)); number 1. start(); number 2 = new Thread(new Number. Printer(2)); number 2. start(); number 3 = new Thread(new Number. Printer(3)); number 3. start(); number 4 = new Thread(new Number. Printer(4)); number 4. start(); number 5 = new Thread(new Number. Printer(5)); number 5. start(); } // main() } // Numbers class
Thread Priority n n ��� set. Priority(n) ���������� Thread ��������� Priority ���������� Thread ����� Priority public Number. Thread(int n) { ������� n =������ 0 -10 num n; } set. Priority(n);
Sleepy Threads n ����� Thread ���������� millisecond n Thread. sleep(1000); //���� 1 ������ ntry { sleep(100); } catch (Interrupted. Exception e) { System. out. println(e. get. Message()); }
Sleepy Threads n n n n n public void run() { for (int k=0; k < 10; k++) { try { Thread. sleep((long)(Math. random() * 1000)); } catch (Interrupted. Exception e) { System. out. println(e. get. Message()); } System. out. print(num); } // for } // run()
Thread States and Life Cycle
wait() notify. All() n n n //not use wait and notify // Thread A public void wait. For. Message() { while (has. Message == false) { Thread. sleep(100); } } // Thread B public void set. Message(String message) {. . . has. Message = true; }
wait() notify. All() n n n n Here's the updated message code: // Thread A public synchronized void wait. For. Message() { try { wait(); } catch (Interrupted. Exception ex) { } } You can also choose to wait for a maximum amount of time. The wait() method can take a maximum amount of time to wait as a parameter: // Thread B public synchronized void set. Message(String message) {. . . notify(); } wait(100);
Stop thread n n n // Create and start the thread My. Thread thread = new My. Thread(); thread. start(); n n // Do work. . . n n n // Stop the thread. all. Done = true; n n n class My. Thread extends Thread { boolean all. Done = false; n // This method is called when the thread runs public void run() { while (true) { // Do work. . . n n n if (all. Done) { return; } n n n n } } } // Do work. . .
Waiting for a Thread to Finish n my. Thread. join(); class test. Jonin. New{ n public static void main(String[] argv){ Socket connection = server. accept( ); ………. Thread input = new Input. Thread(connection. get. Input. Stream( )); input. start( ); Thread output = new Output. Thread(connection. get. Output. Stream( )); output. start( ); // wait for output and input to finish try { input. join( ); output. join( ); } catch (Interrupted. Exception ex) { } } }
Synchronization Why Synchronization public class Maze { private int player. X; private int player. Y; public boolean is. At. Exit() { return (player. X == 0 && player. Y == 0); } } public void set. Position(int x, int y) { player. X = x; player. Y = y; } Let's say you're creating a maze game. Any thread can set the position of the player, and any thread can check to see if the player is at the exit. For simplicity, let's say the exit is at position x = 0, y = 0.
Synchronization n Starting off, the object's variables are player. X = 1 and player. Y = 0. n Thread A calls set. Position(0, 1). n The line player. X = x; is executed. Now player. X = 0. n Thread A is pre-empted by Thread B. n Thread B calls is. At. Exit(). n Currently, player. X = 0 and player. Y = 0, so is. At. Exit() returns true!
Synchronization n public class Maze { private int player. X; private int player. Y; n n public synchronized boolean is. At. Exit() { return (player. X == 0 && player. Y == 0); } n n n n } public synchronized void set. Position(int x, int y) { player. X = x; player. Y = y; }
Synchronization n public synchronized void set. Position(int x, int y) { player. X = x; player. Y = y; } n is essentially the same as this: n n n n n public void set. Position(int x, int y) { synchronized(this) { player. X = x; player. Y = y; } }
Synchronization n n n public void my. Method() { synchronized(this) { // code that needs to be synchronized } // code that is already thread-safe }
- Slides: 26