Java - problema con synchronized

 
Vista:
sin imagen de perfil

problema con synchronized

Publicado por Bea (1 intervención) el 02/12/2016 12:57:16
El programa me ejecuta el ultimo hilo que he iniciado y me ejecuta el método synchronized 3 veces con el mismo hilo, sin llegar a ejecutar los otros dos.

Os dejo el código...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package hilos;
 
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
 
/**
 *
 * @author Bea Arévalo
 */
public class Personaje extends Thread {
 
    static String nombre;
    static int distancia;
    static JLabel label;
 
    Personaje (String nombre, int distancia, JLabel label){
        this.nombre = nombre;
        this.distancia = distancia;
        this.label = label;
    }
 
    @Override
    public void run(){
       correr();
    }
 
    public static void correr(){
        synchronized(Personaje.class) {
 
            for(int n=0; n<distancia ; n++){
 
                try {
                    label.setLocation(n, 0);
                    Thread.sleep(5);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Personaje.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println(distancia);
                System.out.println(n);
                System.out.println(nombre);
            }
        }
    }
}
 
 
 
 
 
package hilos;
 
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
 
/**
 *
 * @author Bea Arévalo
 */
public class Hilos extends JFrame {
 
    JPanel[] paneles;
    JPanel panel;
    JLabel[] labels;
    String[] nombres = {"flanders","bob","homer"};
    JButton boton, boton2,boton3;
 
    Toolkit t = Toolkit.getDefaultToolkit();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int ancho = screenSize.width;
 
    public static void main (String [] args) {
        Hilos h = new Hilos();
    }
 
    public Hilos(){
        setLayout(new GridLayout(4,1));
 
        paneles = new JPanel[3];
        labels = new JLabel[3];
 
        for(int i = 0; i<3; i++){
            paneles[i]= new JPanel();
            add(paneles[i]);
            labels[i]= new JLabel();
            labels[i].setIcon(new ImageIcon(getClass().getResource(nombres[i] + ".gif")));
            paneles[i].add(labels[i]);
            labels[i].setLocation(0,0);
        }
 
        boton =new JButton("CORRER");
 
        boton.addActionListener(new ActionListener(){
 
            @Override
            public void actionPerformed(ActionEvent e){
 
                Personaje homer = new Personaje("Homer", ancho-10, labels[2]);
                homer.start();
 
                Personaje bob = new Personaje("Bob", ancho -10, labels[1]);
                bob.start();
 
                Personaje flanders = new Personaje("Flanders", ancho - 10, labels[0]);
                flanders.start();
            }
        });
 
        add(boton);
 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(200,700);
        setSize(ancho,700);
        setVisible(true);
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

problema con synchronized

Publicado por Tom (1831 intervenciones) el 02/12/2016 13:30:57
Y ¿ Qué te hace pensar que se "ejecuta el método synchronized 3 veces con el mismo hilo" ?
De paso ¿ Qué crees que significa "static" en java ?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar