/ / El subproceso no se ejecuta y por qué jframe setresizeable no funciona: java, multithreading, swing, jframe

El subproceso no se ejecuta, y por qué jframe setresizeable no funciona: java, multihilo, swing, jframe

¿Por qué no funciona este programa? (No imprime "Ejecutando ...")

package eu.inmensia.learn;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Client extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
public static final int HEIGHT = WIDTH / 16 * 9;
public static final short SCALE = 3;

private Thread thread;
private JFrame frame = new JFrame();
private boolean running = false;

public Client() {
Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
setPreferredSize(size);
}

public synchronized void start() {
running = true;
thread = new Thread("display");
thread.start(); // start the thread
}

public synchronized void stop() {
running = false;
try{
thread.join(); // end the thread
}catch(InterruptedException e){ e.printStackTrace(); }
}

public void run() {
while(running){
System.out.println("Running...");
}
}

public static void main(String[] args) {
Client client = new Client();
client.frame.setResizeable(false);
client.frame.setTitle("Program test");
client.frame.add(client);
client.frame.pack();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setLocationRelativeTo(null);
client.frame.setVisible(true);

client.start();
}
}

Estoy tratando de aprender los hilos, y es una cosa, si no la más difícil, que he aprendido. OOP no es nada para esto xD

Respuestas

1 para la respuesta № 1

Debido a esto

new Thread("display");

Cambiarlo a

new Thread(this)

Solo espero que sepas lo que estás haciendo.


2 para la respuesta № 2

haces esto de manera incorrecta, cuando usted llama client.start(); llamará a la función de inicio en el Client class y en esa función crea una nueva instancia de la clase de hilo que tiene el valor predeterminado run método que está vacío

puede que se refiera a este código:

public synchronized void start() {
running = true;
thread = new Thread(this);
thread.start(); // start the thread
}

Espero que esto te ayude


0 para la respuesta № 3

Ha creado un objeto de hilo genérico (leer EN BLANCO). Necesitas pasar tu clase como parámetro.

thread = new Thread(this);

Esto vinculará su método de ejecución al objeto Thread. El nombre del hilo no suele ser tan importante. Referirse a este ejemplo