Java - Threads Java

 
Vista:
sin imagen de perfil

Threads Java

Publicado por Ismael (1 intervención) el 03/04/2015 18:48:32
Buenas tardes, tengo una duda sobre el uso de los Threads.

El caso es que me han mandado un ejercicio que trata sobre una cuenta bancaria donde se puede ingresar i sacar dinero.

El problema lo tengo en que me piden que cuando no haya suficiente dinero en la cuenta, el thread que quiera sacar dinero se espere a que haya suficiente pero además ( y aqui es donde no se como hacerlo) deben "despertarse" los threads en el orden que han solicitado sacar el dinero y por lo que tengo entendido, cuando haces un signal() la eleccion es arbitraria.

Si me pudieran ayudar se lo agradeceira,

Ismael
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Threads Java

Publicado por Tom (1831 intervenciones) el 15/04/2015 18:21:49
Por ejemplo:

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
78
79
80
81
82
83
84
85
86
87
88
package lwp;
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/* */
public class ThrTest extends Thread {
	private volatile long balance;
	private final LinkedBlockingQueue<Condition> orders;
	private final Lock lock;
	private final Condition condRaise, condNext;
	/* */
	ThrTest() {
		balance = 0L;
		orders = new LinkedBlockingQueue<>();
		lock = new ReentrantLock();
		condRaise = lock.newCondition();
		condNext = lock.newCondition();
	}
	/* */
	void get(int amount) {
		Condition cond = lock.newCondition();
		orders.offer(cond);
		try {
			lock.lock();
			cond.await();
			/* */
			while(balance < amount) {
				try {
					condRaise.await();
				} catch(InterruptedException ex) {
				}
			}
			balance -= amount;
			condNext.signal();
		} catch(InterruptedException ex) {
		} finally {
			lock.unlock();
		}
	}
	/* */
	void put(int amount) {
		lock.lock();
		balance += amount;
		condRaise.signalAll();
		lock.unlock();
	}
	/* */
	@Override
	public void run() {
		while(true) {
			try {
				Condition cond = orders.take();
				lock.lock();
				cond.signal();
				try {
					condNext.await();
				} catch(InterruptedException ex) {
				}
				lock.unlock();
			} catch(InterruptedException ex) {
			}
		}
	}
	/* */
	public static void main(String args[]) {
		final ThrTest test = new ThrTest();
 
		test.start();
		for(int i = 0; i < 10; i++) {
			new Thread(() -> {
				System.out.printf("Start %s\n", Thread.currentThread().getName());
				test.get(700);
				System.out.printf("End %s\n", Thread.currentThread().getName());
			}).start();
		}
 
		for(int i = 0; i < 11; i++) {
			test.put(650);
			try {
				Thread.sleep(2L * 1000L);
			} catch(InterruptedException ex) {
			}
		}
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar