Darse cuenta del efecto:
El hombre de hilo rompiendo: (hilo único)
Cuando el hilo principal es procesar una solicitud para actualizar la imagen, no puede aceptar otras solicitudes, cayendo así en un estado de bucle muerto bloqueado.
Dibujar imágenes
import java.awt.graphics; import java.awt.image; import java.awt.toolkit; import javax.swing.jpanel; public class Cartonperson extiende Jpanel implementos runnables {imagen img [] = nueva imagen [6]; int index = 0; int speed; public Cartonperson (int velocidad) {this.speed = speed; img [0] = toolkit.getDefaultToolkit (). getImage ("1.png"); img [1] = toolkit.getDefaultToolkit (). "3.png"); img [3] = toolkit.getDefaultToolkit (). run () {while (true) {try {Repaint (); Thread.sleep (100);} Catch (InterruptedException e) {E.PrintStackTrace ();}}}@overRidePublic Void pinponent (gráfico g) {// TODO Auto-Generado Método stubsuper.PaintComponent (g); g.drawimage (img, 0, INDEX], 0, INDEX, 0, INDEX, 0, INDEDO, 0, INDEDO, 0, INDEDO, INDEDO, INDEDO. 0, getWidth (), getheight (), this); // system.out.println (index); if (index == 5) {index = 0;} else {index ++;}}}}Diseño de formulario de un solo hilo
import java.awt.event.ActionEvent; import java.awt.event.actionListener; import javax.swing.jbutton; import javax.swing.jframe; public class SinglethreadCarton extiende jFrame {Cartonperson P1; JButton bStart = New JButton ("inicio"); JButton BPause = Newperson JButton ("Wait"); JButton Brew = New JButton ("Continuar"); SinglethReadCarton () {init (); this.settitle ("Breaking Threadman"); this.setsize (600, 500); this.setResizable (true); this.setLocationRelativeto (null); this.setDefaultCloseOperation (exit_on_close); this.setVisible (true);} void init () {this.setLayout (null); P1 = new Cartsonperson (0); p1.setBounds (260, 100, 80, 80, 80, 80 160); bStart.setBounds (260, 280, 80, 30); bpause.setBounds (260, 320, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.adDactionListener (260, 360, 80, 30); Bresume.addactionListener (BC);} clase ButtonClick implementa ActionListener {@OverridpidePoid Void Action-Wortformed (ActionVent E) {///1 stubif (e.getSource () == bStart) {p1.run ();} else if (e.getSource () == bpause) {} else if (e.getSource () == Bresume) {}}} public static void main (string [] args) {new SinglethReadCarton ();}}Resultados de ejecución:
Después de hacer clic en el botón "Inicio", el programa se bloquea.
Diseño de forma multiproceso
import java.awt.event.actionEvent; import java.awt.event.actionListener; import javax.swing.jbutton; import javax.swing.jframe; public class multithreadcarton extiende jframe {cartonperson p1; hilo t1; jbutton bstart = new jbutton ("inicio"); jbutton bpAuse = bpause = new = newpaus JButton ("Wait"); JButton Brew = New JButton ("Continuar"); MultithReadCarton () {init (); this.settitle ("Running Threadman"); this.setsize (600, 500); this.setResizable (true); this.setLocationRelativeto (null); this.setDefaultCloseOperation (exit_on_close); this.setVisible (true);} void init () {this.setLayout (null); P1 = new Cartsonperson (0); p1.setBounds (260, 100, 80, 80, 80, 80 160); bStart.setBounds (260, 280, 80, 30); bpause.setBounds (260, 320, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); 360, 80, 30);bresume.setBounds(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30);bresume.addActionListener(260, 360, 80, 30); Bresume.AdDactionListener (260, 360, 80, 30); Bresume.addactionListener (260, 360, ButtonClick implementa ActionListener {@OverridePublic Void ActionPerformed (ActionEvent E) {// TODO AUTO GENERADO stubif (e.getSource () == bStart) {// p1.run (); t1.start ();} else if (e.getSource () == bPause) {t1.suspend ();} else if (e.getSource () == bresume) {t1.ReMum MultithithreadCarton ();}}Resultado de ejecución: como se muestra en la figura superior.
Lo anterior es la descripción del ejemplo del héroe del hilo que puede correr en Swing en Java esta vez. Gracias por su apoyo a Wulin.com.
Este artículo se reproduce en: https://www.idaobin.com/archives/841.html