J
jimmie0
Hello, i'm writing java program using Threads, but i don't quite
understeand some...
My program has 4 threads, each one prints on System.out one letter: A,
B, C or D. There are some rules they must obey.
in any moment number of letters
1. A + B <= C + D
2.A <= 2*B
3. after C next letter C can be print after letter D
using notify, notifyAll, wait
there is my code, i synchronized methods but working program prints
some strange results, maybe you can help me?
Thanks for any help.
there is my code:
public class Th extends Thread
{
private int which;
private static int[] howMany = {0, 0, 0, 0} ;
public Th(int which)
{
this.which= which;
}
public void run()
{
switch (which)
{
case 1: ThA(); break;
case 2: ThB(); break;
case 3: ThC(); break;
case 4: ThD(); break;
}
}
public void ThA()
{
synchronized(this)
{
if ( (howMany[0]+howMany[1]) <
(howMany[2]+howMany[3]) && (howMany[0] <= 2*howMany[1]))
{
System.out.println("A");
howMany[0]++;
}
try {
sleep(500);
}catch(InterruptedException e) {}
ThA();
}
}
public void ThB()
{
synchronized(this)
{
if ( (howMany[0]+howMany[1]) <
(howMany[2]+howMany[3]) )
{
System.out.print("B");
howMany[1]++;
}
try {
sleep(1110);
}catch(InterruptedException e) {}
ThB();
}
}
public void ThC()
{
synchronized(this)
{
System.out.print("C");
howMany[2]++;
try {
wait();
sleep(100);
}catch(InterruptedException e) {}
}
ThC();
}
public void ThD()
{
synchronized(this)
{
System.out.print("D");
howMany[3]++;
try {
notify();
sleep(1000);
}catch(InterruptedException e) {}
}
ThD();
}
public static void main(String[] args)
{
new Th(1).start();
new Th(2).start();
new Th(3).start();
new Th(4).start();
}
}
understeand some...
My program has 4 threads, each one prints on System.out one letter: A,
B, C or D. There are some rules they must obey.
in any moment number of letters
1. A + B <= C + D
2.A <= 2*B
3. after C next letter C can be print after letter D
using notify, notifyAll, wait
there is my code, i synchronized methods but working program prints
some strange results, maybe you can help me?
Thanks for any help.
there is my code:
public class Th extends Thread
{
private int which;
private static int[] howMany = {0, 0, 0, 0} ;
public Th(int which)
{
this.which= which;
}
public void run()
{
switch (which)
{
case 1: ThA(); break;
case 2: ThB(); break;
case 3: ThC(); break;
case 4: ThD(); break;
}
}
public void ThA()
{
synchronized(this)
{
if ( (howMany[0]+howMany[1]) <
(howMany[2]+howMany[3]) && (howMany[0] <= 2*howMany[1]))
{
System.out.println("A");
howMany[0]++;
}
try {
sleep(500);
}catch(InterruptedException e) {}
ThA();
}
}
public void ThB()
{
synchronized(this)
{
if ( (howMany[0]+howMany[1]) <
(howMany[2]+howMany[3]) )
{
System.out.print("B");
howMany[1]++;
}
try {
sleep(1110);
}catch(InterruptedException e) {}
ThB();
}
}
public void ThC()
{
synchronized(this)
{
System.out.print("C");
howMany[2]++;
try {
wait();
sleep(100);
}catch(InterruptedException e) {}
}
ThC();
}
public void ThD()
{
synchronized(this)
{
System.out.print("D");
howMany[3]++;
try {
notify();
sleep(1000);
}catch(InterruptedException e) {}
}
ThD();
}
public static void main(String[] args)
{
new Th(1).start();
new Th(2).start();
new Th(3).start();
new Th(4).start();
}
}