Documente Academic
Documente Profesional
Documente Cultură
POO
Thread #1 Thread #2
Time
public static void main(String[] args) {
HelloThread ht = new HelloThread();
ht.start();
System.out.println("Fim da thread principal");
}
}
public static void main(String[] args) {
HelloThread ht = new HelloThread();
ht.start();
System.out.println("Fim da thread principal");
}
}
public static void main(String[] args) {
HelloThread ht = new HelloThread();
ht.start();
System.out.println("Fim da thread principal");
}
}
ht = new(...)
ht = new(...)
ht.start()
ht = new(...)
ht.start()
println(...) println(...)
ht = new(...)
ht.start()
println(...) println(...)
ht = new(...)
ht.start()
println(...) println(...)
public void run() {
System.out.println("Hello thtread " + nro);
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++ ) {
HelloThread ht = new HelloThread(i);
ht.start();
}
System.out.println("Fim da thread principal");
}
public void run() {
System.out.println("Hello thtread " + nro);
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++ ) {
HelloThread ht = new HelloThread(i); São criadas 100 Threads,
ht.start(); numeradas de 0 a 99
}
System.out.println("Fim da thread principal");
}
Ev
en
to
oc
orr
eu
Ev
en
to
oc
orr
eu
actionPerformed(Event e)
Ev
en
to
oc
orr
eu
actionPerformed(Event e)
Return
Ev
en
to
oc
orr
eu
actionPerformed(Event e)
Return
public AtuaThread(JLabel label, int k) {
jl = label;
n = k;
}
Controlador
Dorme um pouquinho.
Incrementa o slider.
Dorme um pouquinho.
Incrementa o slider.
Quando chegar no limite,
tenta avisar o controlador.
public void setFirst(String s) {
if ( first == null )
first = s;
}
public String getFirst(){
return first;
}
public synchronized void setFirst(String s) {
if ( first == null )
first = s;
}
public synchronized String getFirst(){
return first;
}
public synchronized void setFirst(String s) {
if ( first == null )
first = s; Somente uma thread pode acessar um
método sincronizado do mesmo objeto.
}
public synchronized String getFirst(){
return first;
}
Servidor
Scanner s = new Scanner(cliente.getInputStream());
while (s.hasNextLine()) {
String r = s.nextLine();
System.out.println(r);
saida.println("Recebido: " + r);
}
Scanner s = new Scanner(cliente.getInputStream());
while (s.hasNextLine()) {
String r = s.nextLine();
System.out.println(r);
saida.println("Recebido: " + r);
}
while (teclado.hasNextLine()) {
saida.println(teclado.nextLine());
System.out.println(server.nextLine());
}