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
|
class sommeThrad extends Thread {
static int s = 0;
int y, z;
int[] x;
public sommeThrad(int[] tab, int min, int max) {
x = tab; y = min; z = max;
}
public void run() {
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = y; i <= z; i++) {
s = s + x[i];
System.out.println("la valeur que contient "+ this.getName() +" a "+i+" = "+s);
}
}
}
public class sommeTab {
public static void main(String[] args) {
int tableau[]={1,5,6,3,7,8,9,5};
sommeThrad a = new sommeThrad (tableau, 0, 4);
sommeThrad b = new sommeThrad (tableau, 5, 7);
//demarer les deux Threads
a.start();
b.start();
//pour que le prog n'affiche pas la somme tant que les deux Thread sont en execution
try {
a.join();
b.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("----------------------------------------");
System.out.println(" la somme = " + sommeThrad.s);
}
} |
Partager