aboutsummaryrefslogtreecommitdiff
path: root/CS2452/assign5/src/ResourceWriter.java
blob: 2fb096764bed8e297fbcf038cd48d2cfad94615d (plain)
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
import java.util.concurrent.Semaphore;

public class ResourceWriter implements Runnable
{
	Thread t;
	byte[] res = null;
	Semaphore flg = null;
	Semaphore flg2 = null;

	ResourceWriter(byte[] r, Semaphore s, Semaphore s2)
	{
		res = r;
		flg = s;
		flg2 = s2;
		t = new Thread(this, "ResourceWriter");
		t.start();
	}

	public void run()
	{
		int i;
		byte value = 33;
		boolean running = true;

		System.out.println("ResourceWriter: starts");
		while (running)
		{
			System.out.println("RW in while");
			try
			{
				flg2.acquire();
				flg.acquire();

				for (i = 0; i < res.length; i++ )
					res[i] = value;
				flg.release();
				flg2.release();
			} catch (Exception e0)
			{
				e0.printStackTrace();
			}

			value++ ;
			if (value == 100) value = 33;
			System.out.println("ResourceWriter: updated, waiting 15, 14, ...");
			try
			{
				Thread.sleep(15000);
			} catch (Exception e)
			{
			}
		}
		System.out.println("ResourceWriter: ends");

	}
}