1 / 10

JAVA – sterowanie i wątki

JAVA – sterowanie i wątki. 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 !!!!!

brilliant
Download Presentation

JAVA – sterowanie i wątki

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA – sterowanie i wątki 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++)); } }} 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); }

  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 }

  3. JAVA – sterowanie i wątki 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(); } } } 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");

  4. JAVA – sterowanie i wątki 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 ; } } } 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); }

  5. JAVA – sterowanie i wątki public void run() { while(true) { try{ Thread.sleep(100); } catch(InterruptedException e){ System.err.println("Przerwany");} if (runFlag) t.setText(Integer.toString(licznik++)); } } }//Zadanie 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; }

  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. }

  7. JAVA – sterowanie i wątki 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ż. 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");} } }

  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

  9. JAVA – synchronizacja wątków 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 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

  10. JAVA – sterowanie i wątki

More Related