JAVA – sterowanie i wątki
Click here to load reader
description
Transcript of JAVA – sterowanie i wątki
![Page 1: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/1.jpg)
1
JAVA – sterowanie i wątki
public class A20 extends javax.swing.JApplet implements ActionListener {
private int licznik = 0;private JTextField t = new JTextField(10);private boolean runFlag = true;
private JButton start = new JButton("Start"), onOff = new JButton("Przelacz");
public void init () { Container p = getContentPane(); p.setLayout(new FlowLayout()); p.add(t); p.add(start); p.add(onOff); start.addActionListener(this); onOff.addActionListener(this);}
public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JButton) { if ( ((JButton)e.getSource()).getText() =="Start" ) go(); else runFlag = !runFlag; }}
public void go() { ta funkcja nie odda sterowania !!!!! while(true) { try{ Thread.sleep(100); //każda aplikacja ma swój wątek } catch(InterruptedException e){ System.err.println("Przerwany");}
if (runFlag) t.setText(Integer.toString(licznik++)); }}}
![Page 2: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/2.jpg)
2
JAVA – wątki
public class MThread extends Thread {
private int pozostalo = 5; private static int licznikThread = 0; private int identifyThreadNo = ++licznikThread;
public MThread() { System.out.println("Utworzono watek nr " + identifyThreadNo); }
public void run() { //run jest automatycznie wywoływana przez metodę start while(true) { System.out.println(" Jestem w wątku (" + identifyThreadNo + ") pozostało wejść = " + pozostalo); if (--pozostalo==0) return; //wyjście } }
public static void main(String [] arg){ for(int i = 0; i <= 10; i++) new MThread().start(); // uruchamiamy wątki, nie wszystkie wystartują od razu} // można zauważyć że nie działają w kolejności powołania}
![Page 3: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/3.jpg)
3
JAVA – sterowanie i wątki
public class A21 extends javax.swing.JApplet implements ActionListener {
class Zadanie extends Thread { // wątek jest klasą wewnętrznąprivate int licznik = 0;private boolean runFlag = true;
public Zadanie() { start(); }public void zmianaStanu() { runFlag = !runFlag; }
public void run() { while(true) { try{ Thread.sleep(100); } catch(InterruptedException e){ System.err.println("Przerwany");}
if (runFlag) t.setText(Integer.toString(licznik++)); }}}//Zadanie
private Zadanie wz = null;private JTextField t = new JTextField(10);private JButton start = new JButton("Start"), onOff = new JButton("Przelacz");
public void init () { Container p = getContentPane(); p.setLayout(new FlowLayout()); p.add(t); p.add(start); p.add(onOff); start.addActionListener(this); onOff.addActionListener(this);}
public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JButton) { if ( ((JButton)e.getSource()).getText() =="Start" ) {if (wz == null); wz = new Zadanie();} else if(wz != null) wz.zmianaStanu(); }} }
![Page 4: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/4.jpg)
4
JAVA – sterowanie i wątki
public class A22 extends javax.swing.JApplet implements Runnable, ActionListener {
private Thread wz = null;private int licznik = 0;private boolean runFlag = true;
public void run() { // z interfejsu Runnable while(true) { try{ Thread.sleep(100); } catch(InterruptedException e){ System.err.println("Przerwany");}
if (runFlag) t.setText(Integer.toString(licznik++)); }}
private JTextField t = new JTextField(10);private JButton start = new JButton("Start"), onOff = new JButton("Przelacz");
public void init () { Container p = getContentPane(); p.setLayout(new FlowLayout()); p.add(t); p.add(start); p.add(onOff); start.addActionListener(this); onOff.addActionListener(this);}
public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JButton) { if ( ((JButton)e.getSource()).getText() =="Start" ) { if (wz == null){ wz = new Thread(A22.this); wz.start(); } } else if(wz != null) runFlag =! runFlag ; } } }
![Page 5: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/5.jpg)
5
JAVA – sterowanie i wątki
public class A23 extends javax.swing.JApplet {
class Zadanie extends Thread { private int licznik = 0; private boolean runFlag = true; private JTextField t = new JTextField(10); private JButton b = new JButton("ON/OFF");
public Zadanie() { b.addActionListener(new Akcja()); JPanel p = new JPanel(); p.add(b); p.add(t); getContentPane().add(p);}
class Akcja implements ActionListener { public void actionPerformed(ActionEvent e) { if((e.getSource() instanceof JButton) && ( ((JButton)e.getSource()).getText() =="ON/OFF" )) zmianaStanu(); } }
public void zmianaStanu() { runFlag = !runFlag; }
public void run() { while(true) { try{ Thread.sleep(100); } catch(InterruptedException e){ System.err.println("Przerwany");}
if (runFlag) t.setText(Integer.toString(licznik++)); }}}//Zadanie
![Page 6: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/6.jpg)
6
JAVA – sterowanie i wątki
private Zadanie [] tz = new Zadanie[10];private static boolean running = false;private JLabel et = new JLabel();private JButton start = new JButton("Start");
public void init () { Container p = getContentPane(); p.setLayout(new FlowLayout()); p.add(start); p.add(et);
start.addActionListener(new Zdarzenie());
for(int i = 0; i <= 5; i++) tz[i] = new Zadanie();}
class Zdarzenie implements ActionListener {
public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JButton) { if ( ( ((JButton)e.getSource()).getText() =="Start" ) && (running == false) ) { running = true; et.setText("Ruszyły"); for(int i = 0; i <=5; i++) tz[i].start();} } }
}//zd.}
![Page 7: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/7.jpg)
7
JAVA – sterowanie i wątki
class TDemon extends Thread {
public TDemon() { setDaemon(true); // ustawienie System.out.println(" uruchomiono");}
public void run() { while(true) { yield(); } }}
public class TDemonTest {
public static void main(String [] arg) { TDemon watek = new TDemon();
if (watek.isDaemon()) System.out.println("Jest demonem"); //sprawdzenie try { System.in.read(); } catch (IOException e ) {System.err.println("blad operacji IO");} }
}
Wątek będący demonem zajmuje się obsługiwaniem innych wątków uruchomionych w tym samym procesie, co wątek demona.
Np. HotJava używa do czterech demonów nazwanych "Image Fetcher", które dostarczają obrazków z dysku lub sieci dla wątków, które tego potrzebują.
Jeżeli wszystkie wątki nie będące demonami kończą pracę wątki demony również.
![Page 8: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/8.jpg)
8
JAVA – synchronizacja wątków
public class TBox{
int value;
synchronized int changeValueIn(int stan) {
while (value != 0) {
try { wait(); } catch (InterruptedException e) { }
}
value = stan;
notifyAll();
return stan;
}
synchronized int changeValueOut(int stan) {
while (value == 0) {
try { wait(); } catch (InterruptedException e) { }
}
value = 0;
notifyAll();
return stan;
}
public static void main(String [] arg) {
TBox box = new TBox();
Tin podaj = new Tin(box);
Tou pobierz = new Tou(box);
podaj.start();
pobierz.start();
}
}
Wersja bez synchronizacji nie ma elementów pogrubionych
![Page 9: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/9.jpg)
9
JAVA – synchronizacja wątków
class Tou extends Thread {
public TBox b;
public int time = 5;
public Tou( TBox c) {super(); b =c;}
public void run() {
while(true){
for(int i = 1; i <=3; i++)
{
b.changeValueOut(i);
System.out.println("pobrano = " + i);
}
if (--time==0) return;
}
}
}//Tou
class Tin extends Thread {
public TBox b;
public int time =5;
public Tin( TBox c) {super(); b=c;}
public void run() {
while(true){
for(int i = 1; i <=3; i++)
{
try {
sleep((int)(Math.random() * 100));
} catch(InterruptedException e){
System.err.println("Przerwany");}
b.changeValueIn(i);
System.out.println(" wstawiono "+i );
}
if (--time==0) return;
}
}} //Tin
![Page 10: JAVA – sterowanie i wątki](https://reader038.fdocuments.pl/reader038/viewer/2022100509/56815a94550346895dc80fe8/html5/thumbnails/10.jpg)
10
JAVA – sterowanie i wątki