Olá, estou com dificuldade num trabalho acadêmico com a seguinte proposta: fazer threads atravessarem de um lado para o outro da tela.
se vier uma thread da esquerda pra direta, entao uma trhead que vier da direita pra esquerda tem que esperar a primeira terninar.
criei um codigo da maneira mais facil de se entender. Mas está gerando um deadlock: quando uma thread atravessa pra um lado, a do lado oposto não começa atravessar quando a primeira termina, alguem consegue identificar onde esta o problema?
package CENARIO;
import java.awt.EventQueue;
@SuppressWarnings("serial")
public class Main extends JFrame {
private JPanel contentPane;
int limite=5;
int iThread=0;
ArrayList<classe> Thread = new ArrayList<classe> ();
Semaphore direita= new Semaphore(limite);
Semaphore esquerda= new Semaphore(limite);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JLabel lbCorda = new JLabel("--------------------cordaaaaaaaaaaaaaaa--------------------");
lbCorda.setBounds(70, 84, 292, 14);
contentPane.add(lbCorda);
JButton btnE = new JButton("E");
btnE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
iThread++;
classe
m = new classe("Thread " +
iThread, esquerda, direita, limite);
m.start();
}
});
btnE.setBounds(10, 108, 51, 23);
contentPane.add(btnE);
JButton btnD = new JButton("D");
btnD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
iThread++;
classe
m = new classe("Thread " +
iThread, direita, esquerda, limite);
m.start();
}
});
btnD.setBounds(373, 108, 51, 23);
contentPane.add(btnD);
}
}
package THREADS;
import java.util.concurrent.Semaphore;
import javax.swing.JOptionPane;
public class classe extends Thread{
String nome;
Semaphore minhaDirecao;
Semaphore direcaoOposta;
int limite;
public void msg(Object x, int y){
if(y==0){
System.out.println(x);
}
if(y==1){
JOptionPane.showMessageDialog(null, x, "Mensagem", JOptionPane.PLAIN_MESSAGE);
}
if(y==2){
System.out.println(x);
JOptionPane.showMessageDialog(null, x, "Mensagem", JOptionPane.PLAIN_MESSAGE);
}
}
public classe(String nome, Semaphore minhaDirecao, Semaphore direcaoOposta, int limite){
this.nome=nome;
this.minhaDirecao=minhaDirecao;
this.direcaoOposta=direcaoOposta;
this.limite=limite;
}
public void animacao1(){
int tempo = (int) (Math.random() * 3500);
tempo+=500;
System.out.println(" " + tempo + " de velocidade");
try {
sleep(tempo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void conferir(){
try {
while (this.direcaoOposta.availablePermits()!=limite) {
try {
msg(nome+": Estou esperando, POIS VEM Thread DO OUTRO LADO",0);
this.wait();
} catch (InterruptedException e) {}
}
msg(nome+": JÁ VERIFIQUEM, AGORA VOU",0);
this.minhaDirecao.acquire();
this.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
if(this.minhaDirecao.availablePermits()==0){
msg(nome+": Vou esperar pra nao arrebebtar a corda",0);
}
animacao1();
msg(nome+": ------------------Já FUI!!!",0);
this.minhaDirecao.release();
this.notifyAll();
}
}
public void run(){
conferir();
}
}