Como usar Thread hilos
Java
Publicado el 11 de Abril del 2018 por Rafael Angel (81 códigos)
2.708 visualizaciones desde el 11 de Abril del 2018
Un ejemplo simple acerca del uso de hilos.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
* Ejemplo simple acerca de como usar hilos.
*/
/**
*
* @author Rafael Angel Montero Fernández.
*/
public abstract class Usando_Thread extends Thread
{
//Metodo a implementar en otras clases.
public abstract void hilos_de_ejecusion();
@Override
public void run()
{
try
{
while(true)
{
hilos_de_ejecusion();//Se llama al metodo automaticamente
sleep(1000);//Esperando un segundo.
}//while
}catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}//catch
}//run
}//Usando_Thread
@SuppressWarnings("serial")
class Formulario_para_hilos extends JFrame implements ActionListener
{
private JMenu jmArchivo=new JMenu("Archivo"), jmVer=new JMenu("Ver"), jmAcciones=new JMenu("Acciones");
private JMenuItem jmiSalir=new JMenuItem("Salir"), jmiComenzar=new JMenuItem("Comnenzar"), jmiDetener=new JMenuItem("Detener"), jmiRealizar_otra_tarea=new JMenuItem("Realizar otra tarea");
private JLabel jlDatos=new JLabel("Datos de los hilos.");
private JTextArea jtaDatos=new JTextArea();
private JScrollPane jspDatos;//=new JScrollPane();
private JMenuBar jmbBarra=new JMenuBar();
public Formulario_para_hilos()
{
super("Formulario abstracto.");
this.setSize(325, 370);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(3);
jlDatos.setBounds(5, 0, 300,25);
this.getContentPane().add(jlDatos);
jtaDatos.setBounds(5, 30, 300,300);
jspDatos=new JScrollPane(jtaDatos);
jspDatos.setBounds(5, 30, 300, 300);
this.getContentPane().add(jspDatos);
jmbBarra.add(jmArchivo);
jmArchivo.add(jmiSalir);
jmbBarra.add(jmVer);
jmbBarra.add(jmAcciones);
jmAcciones.add(jmiComenzar);
jmAcciones.add(jmiDetener);
jmAcciones.add(jmiRealizar_otra_tarea);
this.jmiRealizar_otra_tarea.addActionListener(this);
this.jmiComenzar.addActionListener(this);
this.jmiDetener.addActionListener(this);
this.jmiSalir.addActionListener(this);
this.setJMenuBar(jmbBarra);
this.show();
}//Constructor
@Override
public void actionPerformed(ActionEvent evt)
{
click_salir(evt);
this.click_comenzar(evt);
this.click_detener(evt);
this.click_realizar_otra_tarea(evt);
}//public void actionPerformed(ActionEvent evt)
public void click_realizar_otra_tarea(ActionEvent evt)
{
if(evt.getActionCommand().equalsIgnoreCase(this.jmiRealizar_otra_tarea.getText())==true)
{
try
{
String msj=JOptionPane.showInputDialog(this, "Escriba cualquier informacion aqui.","Cualquier informacion");
JOptionPane.showMessageDialog(this, msj);
}catch(Exception ex){JOptionPane.showMessageDialog(null, "No escribió nada en el input.");
}//Catch
}
}//click_realizar_otra_tarea
public void click_detener(ActionEvent evt)
{
if(evt.getActionCommand().equalsIgnoreCase(jmiDetener.getText())==true)
{
this.detener();
}
}//click_detener
public void click_comenzar(ActionEvent evt)
{
if(evt.getActionCommand().equalsIgnoreCase(jmiComenzar.getText())==true)
{
this.comenzar();
}
}//click_comenzar
public void click_salir(ActionEvent evt)
{
if(evt.getActionCommand().equalsIgnoreCase(jmiSalir.getText())==true)
{
System.exit(0);
}
}//click_salir
Mi_hilo hilo=new Mi_hilo();
public void comenzar()
{
try
{
hilo.start();//Cuando se ejecuta por primera vez.
}
catch(Exception ex)
{
hilo.resume();//Ha sido detenido o suspendido y entonces se resume o se continua con el hilo.
}
}//comenzar
public void detener()
{
hilo.suspend();//Mejor se suspende. Es mejor que stop o sea mejor que detenerlo.
}//detener
public class Mi_hilo extends Usando_Thread
{
private int contador=0;
@Override
public void hilos_de_ejecusion() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
contador++;
jtaDatos.setText(jtaDatos.getText() + "Contando="+ contador + "\n");
}//hilos_de_ejecusion
}//Mi_hilo
}//Formulario_para_hilos