Bonjour,

J'essaye de comprendre un peu le fonctionnement d'une tache héritant de SwingWorker et une chose me tracasse, si l'on enlève la boucle while dans le doInBackground de l'exemple compilable ci dessous alors un seul des 7 publish() semble se produire..

Pourquoi ?

Merci de votre aide.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
 
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 
 
 
import java.util.List;
 
@SuppressWarnings("serial")
public class Flipper extends JFrame implements ActionListener {
 
	private final GridBagConstraints constraints;
    private final JTextField headsText, totalText;
    private final Border border = BorderFactory.createLoweredBevelBorder();
    private final JButton startButton, stopButton;
    private FlipTask flipTask;
 
    private JTextField makeText() {
 
    	JTextField t = new JTextField(20);
        t.setEditable(false);
        t.setHorizontalAlignment(JTextField.RIGHT);
        t.setBorder(border);
        getContentPane().add(t, constraints);
        return t;
    }
 
    private JButton makeButton(String caption) {
 
    	JButton b = new JButton(caption);
        b.setActionCommand(caption);
        b.addActionListener(this);
        getContentPane().add(b, constraints);
        return b;
    }
 
    public Flipper() {
 
    	super("Flipper");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Make text boxes
        getContentPane().setLayout(new GridBagLayout());
        constraints = new GridBagConstraints();
        constraints.insets = new Insets(3, 10, 3, 10);
        headsText = makeText();
        totalText = makeText();
 
        //Make buttons
        startButton = makeButton("Start");
        stopButton = makeButton("Stop");
        stopButton.setEnabled(false);
 
        //Display the window.
        pack();
        setVisible(true);
    }
 
 
    private class FlipTask extends SwingWorker<Void, String> {
 
    	@Override
        protected Void doInBackground() {
 
            while(!isCancelled()){
                publish(new Random().toString());
                publish(new Random().toString());
                publish(new Random().toString());
                publish(new Random().toString());
                publish(new Random().toString());
                publish(new Random().toString());
                publish(new Random().toString());
 
            }
			return null;
        }
 
        protected void process(List<String> pairs) {
        	System.out.println("process");
        	String pair = pairs.get(pairs.size() - 1);
            headsText.setText(pair);
            totalText.setText(String.valueOf(pairs.size()));       
        }
    }
 
 
 
    public void actionPerformed(ActionEvent e) {
 
    	if ("Start" == e.getActionCommand()) {
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            (flipTask = new FlipTask()).execute();
        } else if ("Stop" == e.getActionCommand()) {
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            flipTask.cancel(true);
            flipTask = null;
        }
 
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Flipper();
            }
        });
    }
}