0
14 Thread<br />HUREE University ICT<br />Instructor: M.J LEE<br />
Thread<br />1 Process == 1 Program<br />1 Process == some Thread, like methods<br />Thread is method in a program<br />Thr...
3<br />Thread Creation<br />class Top implements Runnable{ <br />    public void run(){ <br />        for(inti=0; i<50; i+...
Thread Creation 2<br />class Top implements Runnable{ <br />    public void run(){ <br />        for(inti=0; i<50; i++) <b...
class RunFrame extends Frame implements Runnable { <br />    public void run() { <br />inti = 0; <br />System.out.println(...
class RunnableFrame extends Frame implements Runnable { <br />    public RunnableFrame() { <br />        new Thread(this)....
Control Threads<br />7<br />Sleep(time)<br />For indicated time, the thread would be in “NotRunable” state. It would get b...
Priority<br />High priority == how often it would be in “Run” state.<br />System Constants<br />Set the priority <br />Get...
Priority Example<br />9<br />class PriorityThreadextends Thread {                         <br />    public void run() { <b...
10<br />public class PriorityThreadMain { <br />public static void main(String[] args) { <br />System.out.println("Main st...
NotRunnable state<br />11<br />Automatically…<br />Be in wait mode for a while<br />Sleep()<br />Intentionally…<br />Contr...
12<br />import java.util.*; <br />class NotRunnableThread extends Thread { <br />public void run() { <br />inti = 0; <br /...
Thread Termination<br />13<br />Generally, run() termination means thread termination<br />
14<br />class TerminateThread extends Thread { <br />private boolean flag = false; <br />    public void run() { <br />int...
15<br />a.start(); <br />b.start(); <br />c.start(); <br />inti; <br />System.out.print(“type thread A, B, C, M?
"); <br /...
Shared Resource : Synchronization<br />16<br />Synchronization<br />The method to share resource<br />To use shared resour...
17<br />class Bank{     private int money = 10000; //balance    public int getMoney(){ return this.money; }     public voi...
18<br />class You extends Thread{     public void run(){         SyncMain.myBank.saveMoney(3000);          System.out.prin...
19<br />public class SyncMain{     public static Bank myBank = new Bank();     public static void main(String[] args) thro...
Upcoming SlideShare
Loading in...5
×

14 thread

741

Published on

Published in: Technology, Health & Medicine
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
741
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "14 thread"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×