2. Thread 1 Process == 1 Program 1 Process == some Thread, like methods Thread is method in a program Threads can be executed simultaneously. How can we execute threads at the same time? simultaneously? 2
3. 3 Thread Creation class Top implements Runnable{ public void run(){ for(inti=0; i<50; i++) System.out.print(i+""); } } public class ThreadMain{ public static void main(String[] args){ System.out.println("getting start!"); Top t = new Top(); Thread thd = new Thread(t); thd.start(); System.out.println("the end"); } } 1 2 3 4 5
4. Thread Creation 2 class Top implements Runnable{ public void run(){ for(inti=0; i<50; i++) System.out.print(i+""); } } public class ThreadMain2{ public static void main(String[] args){ System.out.println("getting start!"); Top t = new Top(); Thread thd1 = new Thread(t); Thread thd2 = new Thread(t); thd1.start(); thd2.start(); System.out.println("the end"); } } 1 2 3 4 5 6 4
5. class RunFrame extends Frame implements Runnable { public void run() { inti = 0; System.out.println("getting start!"); while(i<20) { System.out.print(i + ""); this.setTitle("operating...." + i++); try{ Thread.sleep(300); } catch(InterruptedException e) { System.out.println(e);} } System.out.println("the end!"); } } public class RunFrameMain{ public static void main(String args[]){ RunFrame r = new RunFrame(); r.setSize(300, 100); r.setVisible(true); Thread t = new Thread(r); t.start(); } } 1 5
6. class RunnableFrame extends Frame implements Runnable { public RunnableFrame() { new Thread(this).start(); } public void run() { inti = 0; System.out.println("getting start!"); while(i<20) { System.out.print(i + ""); this.setTitle("operating" + i++); try{ Thread.sleep(300); }catch(InterruptedException e) { System.out.println(e);} } System.out.println("the end!"); } } public class RunnableFrameMain{ public static void main(String args[]){ RunnableFrame r = new RunnableFrame(); r.setSize(300, 100); r.setVisible(true); } } 1 6
7. Control Threads 7 Sleep(time) For indicated time, the thread would be in “NotRunable” state. It would get back in “Runable” state. According to the priorities of threads, one thread would be in “run” state. When the thread be called, its statewould be exchanged automatically. Compulsory, Wait(): move the thread into “NotRunnable” state. Notify(): get back to the “Runable” state When the thread has done his work, it would be in “dead” state
8. Priority High priority == how often it would be in “Run” state. System Constants Set the priority Get the priority of the thread in operation 8 public static final int MIN_PRIORITY = 1; public static final int NORM_PRIORITY = 5; public static final int MAX_PRIORITY = 10; PriorityThread t = new PriorityThread(); t.setPriority(1); //t.setPriority(Thread.MIN_PRIORITY); t.setPriority(5); //t.setPriority(Thread.NORM_PRIORITY); t.setPriority(10);//t.setPriority(Thread.MAX_PRIORITY); int p = t.getPriority();
9. Priority Example 9 class PriorityThreadextends Thread { public void run() { inti = 0; System.out.print(this.getName()); System.out.println("[priority:“ + this.getPriority() + "] start"); while(i < 10000) { i = i + 1; try{ this.sleep(2); }catch(Exception e){System.out.println(e);} } System.out.print(this.getName()); System.out.println("[priority:" + this.getPriority() + "] end"); } }
10. 10 public class PriorityThreadMain { public static void main(String[] args) { System.out.println("Main starts"); for(int i=1; i<=10; i++){ //for(inti=Thread.MIN_PRIORITY;i<=Thread.MAX_PRIORITY; i++) { PriorityThread s = new PriorityThread(); s.setPriority(i); s.start(); } System.out.println("Main ends"); }//end of main } 1 2
11. NotRunnable state 11 Automatically… Be in wait mode for a while Sleep() Intentionally… Control between NotRunnable state and Runnable state Waite(), notify()
12. 12 import java.util.*; class NotRunnableThread extends Thread { public void run() { inti = 0; while(i < 10) { System.out.println(i + "th :" + System.currentTimeMillis() + ""); i = i + 1; try{ this.sleep(1000); }catch(Exception e){System.out.println(e);} } } } public class NotRunnableMain { public static void main(String args[] ) { NotRunnableThread s = new NotRunnableThread(); s.start(); } } 2 1
14. 14 class TerminateThread extends Thread { private boolean flag = false; public void run() { int count = 0; System.out.println(this.getName() +"start"); while(!flag) { try { this.sleep(100); } catch(InterruptedException e) { } } System.out.println(this.getName() +"end"); } public void setFlag(boolean flag){ this.flag = flag; } } public class TerminateMain { public static void main(String args[])throws Exception{ System.out.println("start"); TerminateThread a = new TerminateThread(); TerminateThread b = new TerminateThread(); TerminateThread c = new TerminateThread(); Flag setting for controlling threads 1 2 Flag setting for controlling threads
15. 15 a.start(); b.start(); c.start(); inti; System.out.print(“type thread A, B, C, M?"); while(true){ i = System.in.read(); if(i == 'A'){ a.setFlag(true); }else if(i == 'B'){ b.setFlag(true); }else if(i == 'C'){ c.setFlag(true); }else if(i == 'M'){ a.setFlag(true); b.setFlag(true); c.setFlag(true); System.out.println("main ends"); break; } } } } All threads has been moved in Runnable state. They are ready to run. 3 Read one character like scanf() in C language 4 5 If flag is true...
16. Shared Resource : Synchronization 16 Synchronization The method to share resource To use shared resource in the order Implementation Lock on the resources Wait() notify() public synchronized void saveMoney(int save){ //....shared resource } public void saveMoney(int save){ synchronized(this) { //....shared resource } }
17. 17 class Bank{ private int money = 10000; //balance public int getMoney(){ return this.money; } public void setMoney(int money){ this.money = money; } public synchronized void saveMoney(int save){ int m = this.getMoney(); try{ Thread.sleep(3000); }catch(InterruptedException e){e.printStackTrace();} this.setMoney(m + save); } public void minusMoney(int minus){ synchronized(this){ int m = this.money; try{ Thread.sleep(200); }catch(InterruptedException e){e.printStackTrace();} this.setMoney(m - minus); } } } 1 2 3.1 3.2 Where is the shared resource? In THIS class!!
18. 18 class You extends Thread{ public void run(){ SyncMain.myBank.saveMoney(3000); System.out.println("saveMoney(3000):" + SyncMain.myBank.getMoney()); } } class YourWife extends Thread{ public void run(){ SyncMain.myBank.minusMoney(1000); System.out.println("minusMoney(3000):" + SyncMain.myBank.getMoney()); } } Flag setting for controlling threads 1 2 Flag setting for controlling threads
19. 19 public class SyncMain{ public static Bank myBank = new Bank(); public static void main(String[] args) throws Exception{ You y = new You(); YourWife w = new YourWife(); y.start(); try{ Thread.sleep(200); }catch(InterruptedException e){e.printStackTrace();} yw.start(); } }