Bonjour,

Threads.
Peut-on appeler la méthode d'un thread depuis un autre thread sans nécessité de synchronisation ?

Ce programme est-il correct ?

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
package testthread;
 
public class TestThreadsStart {
 
	/**
         * @param args
         */
 
	private String stringCommun;
 
	public static void main(String[] args) {
		System.out.println("Program Begin");
		ThreadAAA threadAAA = new ThreadAAA();
		ThreadBBB threadBBB = new ThreadBBB(threadAAA);
		threadAAA.start();
		threadBBB.start();
		System.out.println("Program End");
	}
 
}

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
package testthread;
 
public class ThreadAAA extends Thread {
 
	@Override
	public void run() {
		incrementeConmpteur(80, "From ThreadAAA : ");
	}
 
	void incrementeConmpteur(int ctr, String strFrom) {
		for (int i = 0; i < ctr; i++) {
			System.out.println("ctr : " + ctr + "  " + strFrom + i);
		}
 
	};
}

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
package testthread;
 
public class ThreadBBB extends Thread {
 
	ThreadAAA threadAAA;
 
	public ThreadBBB(ThreadAAA threadAAA) {
		this.threadAAA = threadAAA;
	}
 
	@Override
	public void run() {
		threadAAA.incrementeConmpteur(10, "Step 1 From ThreadBBB Target 10 : ");
		threadAAA.incrementeConmpteur(17, "Step 2 From ThreadBBB Target 17 : ");
		threadAAA.incrementeConmpteur(23, "Step 3 From ThreadBBB Target 23 : ");
	}
}