{"id":288,"date":"2013-06-18T16:55:41","date_gmt":"2013-06-18T15:55:41","guid":{"rendered":"http:\/\/chezdom.net\/etu\/?p=288"},"modified":"2013-06-23T16:05:06","modified_gmt":"2013-06-23T15:05:06","slug":"tp-17-juin-2013-threads","status":"publish","type":"post","link":"https:\/\/chezdom.net\/etu\/2013\/06\/18\/tp-17-juin-2013-threads\/","title":{"rendered":"TP 1 &#8211; Threads"},"content":{"rendered":"<h2>Exercice 1<\/h2>\n<p>1. Ecrire une classe <span style=\"font-family: monospace; font-weight: bold;\">Compteur<\/span> qui poss\u00e8de un nom et qui compte jusqu&rsquo;\u00e0 n en affichant \u00e0 chaque fois son nom et le nombre en cours, dans une fonction \u00ab\u00a0<span style=\"font-family: monospace; font-weight: bold;\">public void run()<\/span>\u00ab\u00a0.<\/p>\n<p>Il marque une pause al\u00e9atoire entre chaque nombre (de 0 \u00e0 2000 millisecondes par exemple).<br \/>\nUne fois termin\u00e9 il indique qu&rsquo;il a termin\u00e9 (toujours en affichant son nom).<\/p>\n<p>2. Dans la fonction main, lancez plusieurs compteurs \u00e0 la suite. Que se passe-t-il ? (\u00e9vident)<\/p>\n<pre class=\"brush: java; gutter: true\">public class Compteur01 {\r\n\r\n\tprivate String nom;\r\n\tprivate int max;\r\n\r\n\tpublic Compteur01(String nom, int max) {\r\n\t\tsuper();\r\n\t\tthis.nom = nom;\r\n\t\tthis.max = max;\r\n\t}\r\n\r\n\tpublic void run() {\r\n\t\tfor (int i=1; i&lt;this.max; i++) {\r\n\t\t\tSystem.out.println(this.nom+&quot; &quot;+i);\r\n\t\t\ttry {\r\n\t\t\t\tThread.sleep((int) (Math.random() * 2000));\r\n\t\t\t} catch (InterruptedException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t\tSystem.out.println(this.nom+&quot; termin\u00e9.&quot;);\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tCompteur01 c1=new Compteur01(&quot;Bob&quot;,8);\r\n\t\tCompteur01 c2=new Compteur01(&quot;Jon&quot;,8);\r\n\t\tc1.run();\r\n\t\tc2.run();\r\n\t}\r\n\r\n}<\/pre>\n<p>3. Modifier la classe pour en faire un thread (attention il ne fait plus appeler directement la m\u00e9thode run()).<\/p>\n<pre class=\"brush: java; gutter: true; first-line: 1; highlight: []; html-script: false\">public class Compteur02 extends Thread {\r\n\r\n\tprivate String nom;\r\n\tprivate int max;\r\n\r\n\tpublic Compteur02(String nom, int max) {\r\n\t\tsuper();\r\n\t\tthis.nom = nom;\r\n\t\tthis.max = max;\r\n\t}\r\n\r\n\tpublic void run() {\r\n\t\tfor (int i=1; i&lt;this.max; i++) {\r\n\t\t\tSystem.out.println(this.nom+&quot; &quot;+i);\r\n\t\t\ttry {\r\n\t\t\t\tThread.sleep((int) (Math.random() * 2000));\r\n\t\t\t} catch (InterruptedException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t\tSystem.out.println(this.nom+&quot; termin\u00e9.&quot;);\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tCompteur02 c1=new Compteur02(&quot;Bob&quot;,8);\r\n\t\tCompteur02 c2=new Compteur02(&quot;Jon&quot;,8);\r\n\t\tc1.start();\r\n\t\tc2.start();\r\n\t}\r\n\r\n}<\/pre>\n<p>4. Ajouter une ligne pour afficher \u00ab\u00a0Tout est termin\u00e9\u00a0\u00bb, \u00e0 la fin de la m\u00e9thode main. Que se passe-t-il ?<\/p>\n<pre class=\"brush: java; gutter: true; first-line: 26; highlight: []; html-script: false\">...\r\n\t\tc1.start();\r\n\t\tc2.start();\r\n\t\tSystem.out.println(&quot;Tout est termin\u00e9.&quot;);<\/pre>\n<p>Comment faire ?<\/p>\n<pre class=\"brush: java; gutter: true; first-line: 26; highlight: []; html-script: false\">...\r\n\t\tc1.start();\r\n\t\tc2.start();\r\n\t\ttry {\r\n\t\t\tc1.join();\r\n\t\t\tc2.join();\r\n\t\t} catch (InterruptedException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t\tSystem.out.println(&quot;Tout est termin\u00e9.&quot;);\r\n\t}\r\n\r\n}<\/pre>\n<p>5. Le langage JAVA propose le m\u00e9canisme des interfaces. Modifiez ce programme pour impl\u00e9menter l&rsquo;interface Runnable au lieu d&rsquo;h\u00e9riter de Thread.<\/p>\n<h2>Exercice 2<\/h2>\n<p>On donne la classe suivante, qui permet de g\u00e9rer le solde d&rsquo;un compte bancaire, en effectuant des d\u00e9bits et des cr\u00e9dits.<\/p>\n<pre class=\"brush: java; gutter: true; first-line: 26; highlight: []; html-script: false\">public class CompteBancaire {\r\n\t  private int solde = 0;\r\n\r\n\t  public int getSolde() {\r\n\t    return solde;\r\n\t}\r\n\r\n\tpublic void debite(int op) {\r\n\t  System.out.println(&quot;D\u00e9bit de &quot;+op);\r\n\t  this.solde-=op;\r\n\t}\r\n\r\n\tpublic void credite(int op) {\r\n\t  System.out.println(&quot;Cr\u00e9dit de &quot;+op);\r\n\t  this.solde+=op;\r\n\t}\r\n}<\/pre>\n<p>La classe OperationNulle lance un ou plusieurs threads qui ex\u00e9cutent des op\u00e9rations s&rsquo;annulant (cr\u00e9dit puis d\u00e9bit de la m\u00eame somme).<\/p>\n<pre class=\"brush: java; gutter: true; first-line: 1; highlight: []; html-script: false\">public class OperationsNulles extends Thread {\r\n\r\n\tprivate CompteBancaire compte;\r\n\tprivate static int nbTh = 20;\r\n\tprivate int ID ;\r\n\r\n\tpublic OperationsNulles(CompteBancaire compte, int i) {\r\n\t\tsuper();\r\n\t\tthis.compte = compte;\r\n\t\tthis.ID=i;\r\n\t}\r\n\r\n\tpublic void run() {\r\n\t\twhile (true) {\r\n\t\t\tint s = (int) (Math.random() * 1000);\r\n\t\t\tcompte.credite(s);\r\n\t\t\tcompte.debite(s);\r\n\t\t\tSystem.out.println(&quot;solde &quot;+compte.getSolde()+ &quot; (op\u00e9ration &quot;+this.ID+&quot;)&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tCompteBancaire compte = new CompteBancaire();\r\n\t\tOperationsNulles operations[]=new OperationsNulles[nbTh];\r\n\t\tfor (int i = 0; i &lt; nbTh; i++) {\r\n\t\t\toperations[i] = new OperationsNulles(compte,i);\r\n\t\t\toperations[i].start();\r\n\t\t}\r\n\t}\r\n\r\n}<\/pre>\n<p>Que se passe-t-il lorsqu&rsquo;on lance plusieurs de ces threads en m\u00eame temps ?<br \/>\nComment corriger.<\/p>\n<pre class=\"brush: java; gutter: true; first-line: 13; highlight: []; html-script: false\">\tpublic void run() {\r\n\t\twhile (true) {\r\n\t\t\tint s = (int) (Math.random() * 1000);\r\n\t\t\tsynchronized (compte) {\r\n\t\t\t\tcompte.credite(s);\r\n\t\t\t\tcompte.debite(s);\r\n\t\t\t}\r\n\t\t\tSystem.out.println(&quot;solde &quot;+compte.getSolde()+ &quot; (op\u00e9ration &quot;+this.ID+&quot;)&quot;);\r\n\t\t}\r\n\t}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Exercice 1 1. Ecrire une classe Compteur qui poss\u00e8de un nom et qui compte jusqu&rsquo;\u00e0 n en affichant \u00e0 chaque fois son nom et le nombre en cours, dans une fonction \u00ab\u00a0public void run()\u00ab\u00a0. Il marque une pause al\u00e9atoire entre<span class=\"ellipsis\">&hellip;<\/span> <span class=\"read-more\"><a href=\"https:\/\/chezdom.net\/etu\/2013\/06\/18\/tp-17-juin-2013-threads\/\">Lire la suite &#8250;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-288","post","type-post","status-publish","format-standard","hentry","category-nouakchott"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9qu1A-4E","_links":{"self":[{"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/posts\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":13,"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/posts\/288\/revisions\/337"}],"wp:attachment":[{"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/media?parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/categories?post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chezdom.net\/etu\/wp-json\/wp\/v2\/tags?post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}