t9p3po
1.
• permit executarea simultana a mai multor parti din program
• constituie o versiune redusa a unui proces
• asemanari : ruleaza independent si simultan
• deosebiri : la crearea unui nou proces (fork) este realizata o copie exacta
a procesului parinte : cod + date; la crearea unui fir de executie nu este copiat
decat codul procesului parinte; toate firele de executie au deci acces la aceleasi
date, datele procesului original
• utilitate : executarea unor operatii in fundal
2. Declararea, instantierea si distrugerea firelor de executie
- prin extinderea clasei Thread
- prin implementarea interfetei Runnable
2.1 Creearea firelor de executie prin extindrea clasei Thread
public class MyMain S public static void main(String argsst) S
CntThread cntThread; //declare thread cntThread a new CntThread(); //create thread cntThread.start(); //start thread running try SSystem.in.read();T //wait for keyboard input catch(java.io.IOException e)ST cntThread.stop(); //stop thread
T
T class CntThread extends Thread S public void run() S int ix a 0;
while (true) S
System.out.println("running, ix a " + ix++);
//write count to screen try SThread.sleep(1000);T //sleep 1 second catch(InterruptedException e)ST
T
T
T
2.2 Creearea firelor de executie prin implementarea interfetei Runnable import java.applet.* ; import java.awt.* ;
public class TestThread extends Applet implements Runnable S
Thread mainThread ;
CntThread thread1, thread2;
public void start() S if (mainThread aa null) S mainThread a new Thread(this); mainThread.start();
T
T public void run() S thread1 a new MyThread(this, 2); thread1.start(); thread2 a new MyThread(this, 3); thread2.start();
T public boolean keyDown( Event evt, int key ) S thread1.stop(); thread2.stop(); return(true);
T public void paint( Graphics g) S g.drawString("Ruleaza 3 fire de executie", 10, 10); g.drawString("Contor1 :"+thread1.counter, 10, 30); g.drawString("Contor2 :"+thread2.counter, 10, 50);
T
T
//--------------------------------------------------------------------- class CntThread implements Runnable S
TestThread parent; boolean loop;
Thread cntThread; int step; int counter;
public CntThread(TestThread p, int s) S parent a p; //salvez instanta parinte step a s;
T
public void start() S if (cntThread aa null) S counter a 0; cntThread a new Thread(this); //creez firul de executie pentru numarare cntThread.start(); //lanseaza firul de executie
T
T public void run() S loop a true;
while (loop) S counter +a step; parent.repaint(); try SThread.sleep(1000);T //pauza de 1 sec catch (InterruptedException e) ST
T
T public void stop() S loop a false;
T
T
2.3 Instantierea unui fir de executie : NEW
• mainThread a new Thread(this) ;
• myThread a new MyThreadClass();
2.4 Distrugerea unui fir de executie : STOP, DESTROY myThread a new MyThreadClass(); myThread.start(); myThread.stop(); myThread a null;
Nu este necesara distrugerea explicita a unui fir de executie. Sistemul Java
de colectare a gunoiului se ocupa de acest lucru. El poate fi fortat sa dezaloce
resuresele alocate unui thread prin atribuirea cu null a variabilei care referea
instanta firului de executie
3. Metode pentru firele de executie
• init() - apelata la prima lansare a unui fir de executie, locul unde
se scrie codul de initializare
• start()- apelata la fiecare lansare, dupa operatiile de initializare
• stop()- apelata la terminarea firului de executie, contine codul de
terminare a unui fir de executie
• run()- apelata de metoda start, contine corpul firului de executie
3. Denumirea firelor de executie
• Thread myThread a new Thread(this.”Nume fir”)
• myThread.getName()
• myThread.setName()
3. Sincronizarea firelor de executie
Un obiect sau o metoda pot fi accesate de mai multe fire de executie. Nu exista
insa nici o garantie privind firul de executie care va avea acces la un obiect
la un moment dat, ceea ce poate conduce la rezultate imprevizibile. Pentru a
evita acest lucru se foloseste cuvantul cheie synchronized, care blocheaza un
obiect pe perioada executarii unui bloc de cod. public void incIndex() S synchronized(index) S
//bloc de cod sincronizat, ob. index este blocat index++;
System.out.println(“index a “ + index);
T
T
Cuvantul cheie synchronized poate fi folosit si ca modificator al unei metode,
asigurandu-se utilizarea ei de catre un singur fir de executie la un moment
dat. public void synchronized incIndex() S index++;
System.out.println(“index a “ + index);
T