Galera olhem esse codigo
package client;
import javax.swing.JOptionPane;
public class Class1
{
public Class1()
{
Thread t = new Thread(new Run("Thread 1"), "Sempre");
t.start();
Thread t1 = new Thread(new Run("Thread 2"), "Sempre");
t1.start();
}
public static void main(String[] args)
{
new Class1();
}
class Run
implements Runnable
{
private String nome;
Run(String x)
{
nome = x;
}
public void run()
{
while (true)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
// TODO
}
System.out.println(nome + " : " + Math.random() * 100);
}
}
}
}
Olha como esta saindo o resultado:
Thread 2 : 7.300497875002298
Thread 1 : 89.23030632052101
Thread 2 : 26.27769061525961
Thread 1 : 28.67944254186202
Thread 1 : 60.935482085554014
Thread 2 : 10.163166920967637
Thread 1 : 97.18875484988338
Thread 2 : 10.730325834085463
Thread 2 : 64.91071621725114
Thread 1 : 80.99685353826263
Thread 1 : 31.82829461323221
Thread 2 : 61.35872212132101
Thread 1 : 31.944223142569083
Thread 2 : 21.191124573317655
Thread 2 : 36.64968339744674
Thread 1 : 27.714034946688216
Thread 1 : 21.076466113955906
Thread 2 : 37.6212754543789
Thread 2 : 79.56254279039905
Thread 1 : 35.280417821696794
Thread 1 : 86.8420735678996
Thread 2 : 35.94328652240012
Thread 1 : 96.75562232405247
O correto nao seria 1 sim outro nao um sim outro nao? Como fazer isso?
