Salut,

Voilà j'essaye de faire une émulation de buffer (TP d'université). Je vous mets ci dessous une partie fonctionnelle incomplète (sachant que je gère aussi des délais fictifs d'attente pour chaque producteurs et consommateurs)

Le buffer contient 4 espaces destinés à 4 lettres.

Un producteur envoie en une lettre au buffer, si celui-ci est plein, le thread se met en attente (wait), sinon il l'insere en première position et décale les autres.

Un consommateur extraie une lettre du buffer (il prend la 4ème si elle n'est pas nulle, ou 3ème, ou seconde ou première) mais si celui-ci est vide il se met en attente (wait).

Le programme fonctionne lorsque j'ai:
- 1 consommateur & 1 producteur
- 2 consommateurs & 1 producteur

Cela ne fonctionne pas lorsque j'ai:
- 1 consommateur & 2 producteurs

Voici mon code:

Classe correspondant au buffer:
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
 
class Q {
	int n;
 
	String a = null;
	String b = null;
	String c = null;
	String d = null;
 
	synchronized String get() {
		if(d==null && c==null && b==null && a==null)
			try {
				wait();
			} catch(InterruptedException e) {
				System.out.println("InterruptedException caught");
			}
 
			if(d!=null)
			{
				String temp = null;
				temp = d;
				d = null;
				notify();
				System.out.println("Got: " + temp);
				return temp;				
			}
			else if(c!=null)
			{
				String temp = null;
				temp = c;
				c = null;
				notify();
				System.out.println("Got: " + temp);
				return temp;
			}
			else if(b!=null)
			{
				String temp = null;
				temp = b;
				b = null;
				notify();
				System.out.println("Got: " + temp);
				return temp;
			}
			else if(a!=null)
			{
				String temp = null;
				temp = a;
				a = null;
				notify();
				System.out.println("Got: " + temp);
				return temp;
			}
			else
			{
				return null;
			}
	}
	synchronized void put(String n) {
		if(a!=null && b!=null && c!=null && d!=null)
			try {
				wait();
			} catch(InterruptedException e) {
				System.out.println("InterruptedException caught");
			}
 
			if(d==null || c==null || b==null || a==null)
			{
				d = c;
				c = b;
				b = a;
				a = n;
			}
			System.out.println("Put: " + n);
			notify();
	}
}
Producteur et consommateur:
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
 
class Producer implements Runnable {
	Q q;
 
	public String processedString = "";
 
	private String getNextCharacter() {
 
		String temp = processedString;
 
		String cs = null;
 
		if(temp!=null)
		{
			try {
			cs = temp.substring(0,1);
			processedString = temp.substring(1, temp.length());
			} catch(Exception e) {
				return null;
			}
		}
 
		return cs;
	}	
 
	Producer(Q q, String s) {
		this.processedString = s;
		this.q = q;
		new Thread(this, "Producer"+s).start();
	}
	public void run() {
		while(true) {
			String character = this.getNextCharacter();
			if(character!=null)
				q.put(character);
		}
	}
}
 
class Consumer implements Runnable {
	Q q;
	Consumer(Q q) {
		this.q = q;
		new Thread(this, "Consumer").start();
	}
	public void run() {
		while(true) {
			q.get();
		}
	}
}
Main:
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
 
class PCFixed {
	public static void main(String args[]) {
		Q q = new Q();
		new Producer(q,"IVEHEARDTHEREWASASECRETCHORD");
		//new Producer(q, "XYXYXYXYXYXYXYXYXYX");
		new Consumer(q);
		//new Consumer(q);
		System.out.println("Press Control-C to stop.");
	}
}
 
class Test
{
	public static void main(String args[])
	{
		PCFixed p = new PCFixed();
		p.main(args);
	}
}
Toute aide est grandement appréciée , je vais aussi chercher de mon coté cet après midi.