threads
Post on 11-Nov-2014
3.898 Views
Preview:
DESCRIPTION
TRANSCRIPT
Threads
Elenilson Vieira
Grupo de Estudos da UFPB para a SCJPelenilson.vieira.filho@gmail.com
O que é Thread?
Linha de Execução
Um processo A tem duas Thread Um processo A tem duas linhas de execução
Extendendo Thread
class MyThread extends Thread {
public void run() {
System.out.println("Important job running in MyThread");
}
}
Implementando Runnable
class MyRunnable implements Runnable {
public void run() {
System.out.println("Important job running in MyRunnable");
}
}
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
Criação de Threads
Uma instância da classe Thread é apenas uma instância como qualquer outra
Thread é uma linha de execução Precisa ser startada
Método start()
Em Threads
A classe Thread possui vários métodos
Para o exame precisamos entender start() yield() sleep() run()
class NameRunnable implements Runnable {
public void run() {
System.out.println("NameRunnable running");
System.out.println("Run by "
+ Thread.currentThread().getName());
}
}
public class NameThread {
public static void main (String [] args) {
NameRunnable nr = new NameRunnable();
Thread t = new Thread(nr);
t.setName("Fred");
t.start();
}
}
Running this code produces the following, extra special, output:
% java NameThread
NameRunnable running
Run by Fred
public class NameThread {
public static void main (String [] args) {
NameRunnable nr = new NameRunnable();
Thread t = new Thread(nr);
// t.setName("Fred");
t.start();
}
}
Running the preceding code now gives us
% java NameThread
NameRunnable running
Run by Thread-0
public class NameThreadTwo {
public static void main (String [] args) {
System.out.println("thread is "
+ Thread.currentThread().getName());
}
}
which prints out
% java NameThreadTwo
thread is main
class NameRunnable implements Runnable {
public void run() {
for (int x = 1; x <= 3; x++) {
System.out.println("Run by "
+ Thread.currentThread().getName()
+ ", x is " + x);
}
}
}
public class ManyNames {
public static void main(String [] args) {
// Make one Runnable
NameRunnable nr = new NameRunnable();
Thread one = new Thread(nr);
Thread two = new Thread(nr);
Thread three = new Thread(nr);
one.setName("Fred");
two.setName("Lucy");
three.setName("Ricky");
one.start();
two.start();
three.start();
}
}
Isso é o resultado?
Running this code might produce the following:
% java ManyNames
Run by Fred, x is 1
Run by Fred, x is 2
Run by Fred, x is 3
Run by Lucy, x is 1
Run by Lucy, x is 2
Run by Lucy, x is 3
Run by Ricky, x is 1
Run by Ricky, x is 2
Run by Ricky, x is 3
Nem Sempre!
O escalonador decide
Cada Thread vai ter o processador a cada fatia de tempo
Não necessariamente a primeira criada será a primeira escolhida
sleep(<long>)
try {
Thread.sleep(5*60*1000); // Sleep for 5 minutes
} catch (InterruptedException ex) { }
class NameRunnable implements Runnable {
public void run() {
for (int x = 1; x < 4; x++) {
System.out.println("Run by "
+ Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException ex) { }
}
}
}
public class ManyNames {
public static void main (String [] args) {
// Make one Runnable
NameRunnable nr = new NameRunnable();
Thread one = new Thread(nr);
one.setName("Fred");
Thread two = new Thread(nr);
two.setName("Lucy");
Thread three = new Thread(nr);
three.setName("Ricky");
one.start();
two.start();
three.start();
}
}
Running this code shows Fred, Lucy, and Ricky alternating nicely:
% java ManyNames
Run by Fred
Run by Lucy
Run by Ricky
Run by Fred
Run by Lucy
Run by Ricky
Run by Fred
Run by Lucy
Run by Ricky
Prioridades
Valor de 1 a 10
Default é 5
Constantes Thread.MIN_PRIORITY (1) Thread.NORM_PRIORITY (5) Thread.MAX_PRIORITY (10)
Método yield()
Uma determinada thread pode ceder o restante de sua fatia de tempo com o processador para outra thread
Método join()
Indica para uma Thread A que ela deve parar
Thread A apenas continua quando a Thread B acabar
import java.util.*;
public class NameList {
private List names = Collections.synchronizedList(
new LinkedList());
public void add(String name) {
names.add(name);
}
public String removeFirst() {
if (names.size() > 0)
return (String) names.remove(0);
else
return null;
}
}
public static void main(String[] args) {
final NameList nl = new NameList();
nl.add("Ozymandias");
class NameDropper extends Thread {
public void run() {
String name = nl.removeFirst();
System.out.println(name);
}
}
Thread t1 = new NameDropper();
Thread t2 = new NameDropper();
t1.start();
t2.start();
}
Thread t1 executes names.size(), which returns 1.
Thread t1 executes names.remove(0), which returns Ozymandias.
Thread t2 executes names.size(), which returns 0.
Thread t2 does not call remove(0).
The output here is
Ozymandias
null
import java.util.*;
public class NameList {
private List names = new LinkedList();
public synchronized void add(String name) {
names.add(name);
}
public synchronized String removeFirst() {
if (names.size() > 0)
return (String) names.remove(0);
else
return null;
}
}
1. class ThreadA {
2. public static void main(String [] args) {
3. ThreadB b = new ThreadB();
4. b.start();
5.
6. synchronized(b) {
7. try {
8. System.out.println("Waiting for b to complete...");
9. b.wait();
10. } catch (InterruptedException e) {}
11. System.out.println("Total is: " + b.total);
12. }
13. }
14. }
16. class ThreadB extends Thread {
17. int total;
18.
19. public void run() {
20. synchronized(this) {
21. for(int i=0;i<100;i++) {
22. total += i;
23. }
24. notify();
25. }
26. }
27. }
wait() e wait(<long>)
will have to get the lock for the object:
synchronized(a){ // The thread gets the lock on 'a'
a.wait(2000); // Thread releases the lock and waits for notify
// only for a maximum of two seconds, then goes back to Runnable
// The thread reacquires the lock
// More instructions here
}
notify() e notifyAll()
notify() Notifica uma thread esperando
notifyAll() Notifica todas e quem consegui o processador vai
executar
top related