Buscar

14POO aula10 doc01

Prévia do material em texto

Programa Principal - BufferRotativo 
 
Vamos ver o código do programa principal: 
 
public class BufferRotativo extends javax.swing.JFrame { 
 
 /** Creates new form BufferRotativo */ 
 public BufferRotativo() { 
 super("Demonstração de Threads Sincronizadas"); 
 initComponents(); 
 // setSize(700,500); 
 // iniciaProducao(); 
 TrataMovimento(null); 
 trataMovimentoConsumidor(null); 
 } 
 
 /** This method is called from within the constructor to 
 * initialize the form. 
 * WARNING: Do NOT modify this code. The content of this method is 
 * always regenerated by the Form Editor. 
 */ 
 @SuppressWarnings("unchecked") 
 // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
 private void initComponents() { 
 
 jScrollPane1 = new javax.swing.JScrollPane(); 
 areaSaida = new javax.swing.JTextArea(); 
 jButton1 = new javax.swing.JButton(); 
 jSliderProd = new javax.swing.JSlider(); 
 jSliderConsum = new javax.swing.JSlider(); 
 jLabel1 = new javax.swing.JLabel(); 
 jLabel2 = new javax.swing.JLabel(); 
 jLblTP = new javax.swing.JLabel(); 
 jLblTC = new javax.swing.JLabel(); 
 jButton2 = new javax.swing.JButton(); 
 jSlider1 = new javax.swing.JSlider(); 
 
 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
 
 areaSaida.setColumns(20); 
 areaSaida.setEditable(false); 
 areaSaida.setRows(5); 
 jScrollPane1.setViewportView(areaSaida); 
 
 jButton1.setText("Sair"); 
 jButton1.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 jButton1ActionPerformed(evt); 
 } 
 }); 
 
 jSliderProd.setMaximum(20); 
 
 jSliderProd.setMinimum(1); 
 jSliderProd.setValue(1); 
 jSliderProd.addMouseListener(new java.awt.event.MouseAdapter() { 
 public void mouseReleased(java.awt.event.MouseEvent evt) { 
 TrataMovimento(evt); 
 } 
 }); 
 
 jSliderConsum.setMaximum(10); 
 jSliderConsum.setMinimum(1); 
 jSliderConsum.setValue(3); 
 jSliderConsum.addMouseListener(new java.awt.event.MouseAdapter() { 
 public void mouseReleased(java.awt.event.MouseEvent evt) { 
 trataMovimentoConsumidor(evt); 
 } 
 }); 
 
 jLabel1.setText("Tempo de trabalho Produtor"); 
 
 jLabel2.setText("Tempo de trabalho Consumidor"); 
 
 jButton2.setText("Inicia Producao"); 
 jButton2.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 jButton2ActionPerformed(evt); 
 } 
 }); 
 
 jSlider1.setValue(0); 
 
 javax.swing.GroupLayout layout = new 
javax.swing.GroupLayout(getContentPane()); 
 getContentPane().setLayout(layout); 
 layout.setHorizontalGroup( 
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, 
layout.createSequentialGroup() 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addGroup(layout.createSequentialGroup() 
 .addContainerGap(259, Short.MAX_VALUE) 
 .addComponent(jButton1)) 
 .addGroup(layout.createSequentialGroup() 
 .addGap(46, 46, 46) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addComponent(jLabel1) 
 .addComponent(jLabel2) 
 .addGroup(layout.createSequentialGroup() 
 .addComponent(jSliderProd, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
 .addComponent(jLblTP, 
javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)) 
 .addGroup(layout.createSequentialGroup() 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
, false) 
 .addComponent(jButton2, 
javax.swing.GroupLayout.Alignment.LEADING, 
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 
Short.MAX_VALUE) 
 .addComponent(jSliderConsum, 
javax.swing.GroupLayout.Alignment.LEADING, 
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 
Short.MAX_VALUE) 
 .addComponent(jSlider1, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE)) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
 .addComponent(jLblTC, 
javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))))) 
 .addGap(10, 10, 10) 
 .addComponent(jScrollPane1, 
javax.swing.GroupLayout.PREFERRED_SIZE, 513, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
 .addContainerGap()) 
 ); 
 layout.setVerticalGroup( 
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addGroup(layout.createSequentialGroup() 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, 
layout.createSequentialGroup() 
 .addContainerGap() 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
) 
 .addGroup(layout.createSequentialGroup() 
 .addGap(107, 107, 107) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addComponent(jLblTP, 
javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE) 
 .addComponent(jSliderProd, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE)) 
 .addGap(35, 35, 35) 
 .addComponent(jLabel2) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
) 
 .addComponent(jLblTC, 
javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE) 
 .addComponent(jSliderConsum, 
javax.swing.GroupLayout.Alignment.LEADING, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE)) 
 .addGap(35, 35, 35) 
 .addComponent(jButton2) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, 
Short.MAX_VALUE).addComponent(jSlider1, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
 .addGap(22, 22, 22) 
 .addComponent(jButton1)) 
 .addComponent(jScrollPane1, 
javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))) 
 .addGroup(layout.createSequentialGroup() 
 .addGap(98, 98, 98) 
 .addComponent(jLabel1))) 
 .addContainerGap()) 
 ); 
 
 pack(); 
 }// </editor-fold> 
 
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
 System.exit(0); 
 } 
 
 private void TrataMovimento(java.awt.event.MouseEvent evt) { 
 jLblTP.setText(Float.toString(((float)jSliderProd.getValue())/2)+" s"); 
 } 
 
 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
 jButton2.setEnabled(false); 
 iniciaProducao(((float)jSliderProd.getValue())/2,jSliderConsum.getValue()); 
 } 
 
 private void trataMovimentoConsumidor(java.awt.event.MouseEvent evt) { 
 
 jLblTC.setText(Integer.toString(jSliderConsum.getValue())+" s"); 
 } 
 
 
 /** 
 * @param args the command line arguments 
 */ 
 public static void main(String args[]) { 
 java.awt.EventQueue.invokeLater(new Runnable() { 
 public void run() { 
 new BufferRotativo().setVisible(true); 
 } 
 }); 
 } 
 
 // Variables declaration - do not modify 
 private javax.swing.JTextArea areaSaida; 
 private javax.swing.JButton jButton1; 
 private javax.swing.JButton jButton2; 
 private javax.swing.JLabel jLabel1; 
 private javax.swing.JLabel jLabel2; 
 private javax.swing.JLabel jLblTC; 
 private javax.swing.JLabel jLblTP; 
 private javax.swing.JScrollPane jScrollPane1; 
 private javax.swing.JSlider jSlider1; 
 private javax.swing.JSlider jSliderConsum; 
 private javax.swing.JSlider jSliderProd; 
 // End of variables declaration 
 
 private void iniciaProducao() { 
 BufferInteiroSincronizado objetoCompartilhado = 
 new BufferInteiroSincronizado(this); 
 ProdutorInteiro produtor = new ProdutorInteiro(objetoCompartilhado, this); 
 ConsumidorInteiro consumidor = new ConsumidorInteiro(objetoCompartilhado, 
this); 
 
 produtor.start(); 
 consumidor.start(); 
 } 
 private void iniciaProducao(float tp, float tc) { 
 
 
 BufferInteiroSincronizado objetoCompartilhado = 
 new BufferInteiroSincronizado(this); 
 ProdutorInteiro produtor = new ProdutorInteiro(objetoCompartilhado, this,tp); 
 ConsumidorInteiro consumidor = new ConsumidorInteiro(objetoCompartilhado, 
this,tc); 
 
 produtor.start(); 
 consumidor.start(); 
 } 
 public void habilitaProducao(boolean h){ 
 
 jButton2.setEnabled(h); 
 } 
 public void adiciona(String s){ 
 areaSaida.append(s); 
 } 
 
} 
 
Podemos observar que tudo que queremos fazer de atualização através dos Threads, 
precisa ser escrito e colocado em métodos. Isso foi feito com o iniciaProdução() e o 
método adiciona().

Continue navegando