Bonjour,
J'aimerais savoir pourquoi, dans cet exemple donné, le modificateur "synchronized" ne permet pas d'afficher entièrement "BONJOUR" puis "AU REVOIR" ? Que faudrait-il changer dans ce code pour que cela soit le cas ?
Merci pour vos réponses.
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 class Main { public static void main(String[] arg) { new Afficher("BONJOUR").start(); new Afficher("AU REVOIR").start(); } } class Afficher extends Thread { String txt; public Afficher (String t) { txt=t; } synchronized public void run() { for (int j=0;j<2;j++) { for (int i=0;i<txt.length();i++) { try { sleep(100); } catch (InterruptedException e) {}; System.out.print(txt.charAt(i)); } } } }
Partager