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++));
} }}
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 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();
}} }
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);
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 ; }
} }
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
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();}
} }
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ż.
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 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