public class Counter 1 extends JApplet private int
第六讲-多线程的概念 public class Counter 1 extends JApplet { private int count = 0; private JButton start = new JButton("Start"), on. Off = new JButton("Toggle"); private JText. Field t = new JText. Field(10); private boolean run. Flag = true; public void init() { Container cp = get. Content. Pane(); cp. set. Layout(new Flow. Layout()); cp. add(t); start. add. Action. Listener(new Start. L()); cp. add(start); on. Off. add. Action. Listener(new On. Off. L()); 4 cp. add(on. Off);
public void go() { while (true) { try { Thread. sleep(100); } catch(Interrupted. Exception e) { System. err. println("Interrupted"); } if (run. Flag) t. set. Text(Integer. to. String(count++)); } } class Start. L implements Action. Listener { public void action. Performed(Action. Event e) { go(); } } 5
public class Simple. Thread extends Thread { private int count. Down = 5; private static int thread. Count = 0; private int thread. Number = ++thread. Count; public Simple. Thread() { System. out. println("Making " + thread. Number); } public void run() { while(true) { System. out. println("Thread " + thread. Number + "(" + count. Down + ")"); if(--count. Down == 0) return; } } public static void main(String[] args) { for(int i = 0; i < 5; i++) new Simple. Thread(). start(); System. out. println("All Threads Started"); } } 7
输出结果: Making 1 Making 2 Making 3 Making 4 Making 5 Thread 1(5) Thread 1(4) Thread 1(3) Thread 1(2) Thread 2(5) Thread 2(4) Thread 2(3) Thread 2(2) Thread 2(1) Thread 1(1) 8
All Threads Started Thread 3(5) Thread 4(4) Thread 4(3) Thread 4(2) Thread 4(1) Thread 5(5) Thread 5(4) Thread 5(3) Thread 5(2) Thread 5(1) Thread 3(4) Thread 3(3) Thread 3(2) Thread 3(1) 9
现在,我们可以用多线程解决前面第一个例子的问题了: public class Counter 2 extends JApplet { private class Separate. Sub. Task extends Thread { private int count = 0; private boolean run. Flag = true; Separate. Sub. Task() { start(); } void invert. Flag() { run. Flag = !run. Flag; } public void run() { while (true) { try {sleep(100); } catch(Interrupted. Exception e) { System. err. println("Interrupted"); } if(run. Flag) t. set. Text(Integer. to. String(count++)); } } } 10
private Separate. Sub. Task sp = null; private JText. Field t = new JText. Field(10); private JButton start = new JButton("Start"), on. Off = new JButton("Toggle"); class Start. L implements Action. Listener { public void action. Performed(Action. Event e) { if(sp == null) sp = new Separate. Sub. Task(); } } class On. Off. L implements Action. Listener { public void action. Performed(Action. Event e) { if(sp != null) sp. invert. Flag(); } } 11
public void init() { Container cp = get. Content. Pane(); cp. set. Layout(new Flow. Layout()); cp. add(t); start. add. Action. Listener(new Start. L()); cp. add(start); on. Off. add. Action. Listener(new On. Off. L()); cp. add(on. Off); } public static void main(String[] args) { Console. run(new Counter 2 (), 300, 100); } } 12
把线程和程序主类相结合: public class Counter 3 extends JApplet implements Runnable { private int count = 0; private boolean run. Flag = true; private Thread self. Thread = null; private JButton start = new JButton("Start"), on. Off = new JButton("Toggle"); private JText. Field t = new JText. Field(10); public void run() { while (true) { try { self. Thread. sleep(100); } catch(Interrupted. Exception e) { System. err. println("Interrupted"); } if(run. Flag) t. set. Text(Integer. to. String(count++)); } } 13
class Start. L implements Action. Listener { public void action. Performed(Action. Event e) { if(self. Thread == null) { self. Thread = new Thread(Counter 3. this); self. Thread. start(); // 将实现了Runnable的Counter 3对象(Counter 3. this)传入一 个线程,然后执行该线程的start, 进而调用run方法 } } } class On. Off. L implements Action. Listener { public void action. Performed(Action. Event e) { run. Flag = !run. Flag; } } 14
public void init() { Container cp = get. Content. Pane(); cp. set. Layout(new Flow. Layout()); cp. add(t); start. add. Action. Listener(new Start. L()); cp. add(start); on. Off. add. Action. Listener(new On. Off. L()); cp. add(on. Off); } public static void main(String[] args) { Console. run(new Counter 3(), 300, 100); } } 15
产生多个线程: public class Counter 4 extends JApplet { private JButton start = new JButton("Start"); private boolean started = false; private Ticker[] s; private int size = 12; class Ticker extends Thread { private JButton b = new JButton("Toggle"); private JText. Field t = new JText. Field(10); private int count = 0; private boolean run. Flag = true; public Ticker() { b. add. Action. Listener(new Toggle. L()); JPanel p = new JPanel(); p. add(t); p. add(b); get. Content. Pane(). add(p); } 16
class Toggle. L implements Action. Listener { public void action. Performed(Action. Event e) { run. Flag = !run. Flag; } } public void run() { while (true) { if (run. Flag) t. set. Text(Integer. to. String(count++)); try { sleep(100); } catch(Interrupted. Exception e) { System. err. println("Interrupted"); } } 17
class Start. L implements Action. Listener { public void action. Performed(Action. Event e) { if(!started) { started = true; for (int i = 0; i < s. length; i++) s[i]. start(); }} } public void init() { Container cp = get. Content. Pane(); cp. set. Layout(new Flow. Layout()); s = new Ticker[size]; for (int i = 0; i < s. length; i++) s[i] = new Ticker(); start. add. Action. Listener(new Start. L()); cp. add(start); } 18
public static void main(String[] args) { Counter 4 applet = new Counter 4(); Console. run(applet, 200, applet. size * 50); } } 19
//this class writes into the pipe until it is full, at which point it is //blocked until the consumer takes something out. Class Producer extends Thread{ proctected Pipe. Output. Stream po=new Pipi. Out. Stream(); private Data. Output. Stream dos=new Data. Output. Stream(po); public void run(){ //just keep producing numbers that represent the //amount of millisec program has been running. for( ; ; ) produce(); } private final long start=System. current. Time. Millis(); private final long banana(){ return (System. current. Time. Millis()-start); } 27
void produce(){ long t=banana(); System. out. println(“produced”+t); try {dos. write. Long(t); } catch (IOException ie){ System. out. println(ie); } } } //This class consumes everything sent over the pipe. Class Consumer extend Thread{ private Piped. Input. Stream pip; private Data. Input. Stream d; //java constructor idiom, save argument. Consumer(Producer who){ try{ 28
pip=new Piped. Input. Stream(who. po); d=new Data. Input. Stream(pip); }catch (IOException ie){ System. out. println(ie); } } long get(){ long i=0; try { i=d. read. Long(); //read from pipe. } catch(IOException ie){System. out. println(ie); } return i; } public void run(){ java. util. Random r=new java. util. Random(); for( ; ; ){ long result=get(); 29
System. out. println(“cpmsi, ed”+result); //next lines are just to make things asynchronous int randomtime=r. next. Int()%1250; try{sleep(randomtime); } chath(Exception e){} } 30
- Slides: 30