//file VariasThreads.java import javax.swing.*; import java.awt.event.*; import java.awt.*; class Escrita implements Runnable { private int i; private static int cont=0; private int identificacao; static JTextArea textArea = new JTextArea(20,60); static JScrollPane scroller = new JScrollPane(textArea); public void run() { while(true) { String result = ("Thread Número (" + identificacao + ") " + i ++ + "\n"); VariasThreads2.escreveResultado(result); try { Thread.sleep(500); } catch(InterruptedException e) { } } } public Escrita() { cont++; identificacao = cont; } } public class VariasThreads2 { static JTextArea textArea = new JTextArea(10,20); static JScrollPane scroller = new JScrollPane(textArea); public static void main(String[] args) { new Thread(new Escrita()).start(); new Thread(new Escrita()).start(); JOptionPane.showMessageDialog(null, scroller, "Várias Threads 2", JOptionPane.PLAIN_MESSAGE); } public static void escreveResultado(String result) { textArea.append(result); } }
Vitor Yudi Hansen