JButton + Thread: quase lá

1 resposta
Rage
O objetivo é iniciar e pausar uma thread através de jbuttons: Classe Swing que chama a tarefa:
public class MainGuiTest extends javax.swing.JFrame {    

    private Thread t = new Thread(new TarefaContinua());
	
    public MainGuiTest() {
        initComponents();
    }   
      
    public static void main(String args[]) {
        new MainGuiTest().setVisible(true);
    }       
	
    ActionListener ActionStart = new ActionListener() {		    
	     public void actionPerformed(ActionEvent e) {
		     t.start();		
	     }
    };

    ActionListener ActionPause = new ActionListener() {		    
		public void actionPerformed(ActionEvent e) {           	
			jTextArea1.append("\nProcesso em espera...");           	
           	try{
           		t.wait();
           	}catch(InterruptedException ie){
           		jTextArea1.append("\nErro: "+ie.getMessage());
           	}
        }           							
	};
	
	ActionListener ActionResume = new ActionListener() {		    
		public void actionPerformed(ActionEvent e) {           	
			jTextArea1.append("\nProcesso retomado...");
      		t.notify();           	
        }           							
	};
Classe que executa a tarefa:
public class TarefaContinua implements Runnable {	
	public void run() {
         for(int w=1; w<=5000; w++){
					MainGuiTest.jTextArea1.append("\nec.VerificaIndividuos();"+w);
		 }
    }
}
Quando clico no botão START, a tarefa é iniciada, e a JTextArea começa a ser preenchida, ok... Quando clico no botão PAUSE, dá o seguinte erro:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException: current thread not owner...
Entendo, mas porque não? Ele está dando preferência para a Thread do Swing, é isso? Porque eu já criei uma Thread separada apenas para a tarefa, para não misturar com a Thread do Swing... Alguém sabe porque a Thread t não está conseguindo ser pausada? []´s!

1 Resposta

Omeganosferatu

o método wait só pode ser chamado dentro de um bloco synchronized, pra você chamar o wait voce precisa obter o " lock" do objeto

tenta algo como

synchronized ( this ) {

try{

t.wait();

}catch(InterruptedException ie){

jTextArea1.append("\nErro: "+ie.getMessage());

}

}
Criado 31 de março de 2008
Ultima resposta 31 de mar. de 2008
Respostas 1
Participantes 2