Threads interruption

J

jimmie0

Hello.
I have 4 threads, they're writing on System.out in while(true) loop one
letter: A, B, C or D.
A parent thread read from System.in and according to input have to stop
some of threads. But it doesn't work - threads still printing letters.
I don't know how to stop these threads. can you help me?

here is my parent thread class:

class Klawiatura extends Thread
{
int n;

static void ustaw(int t)
{
switch (t)
{
case 0: Th.end[0] = false;
case 1: Th.end[1] = false;
case 2: {Th.end[0] = false;Th.end[1] = false;}

}
}

public void run()
{
BufferedReader i = new BufferedReader(new
InputStreamReader(System.in));

Th Th1 = new Th(1);
Th Th2 = new Th(2);
Th Th3 = new Th(3);
Th Th4 = new Th(4);

Th1.start();
Th2.start();
Th3.start();
Th4.start();



while(true)
{
try
{
n = i.read();
switch (n)
{
case 49: Klawiatura.ustaw(0);
case 50: [1] = false;
case 51: Th1.end[0] = false;
}
System.out.println("N: "+n);
}
catch(IOException e)
{}
}
}
}
 
J

jimmie0

here is whole program without my errors

import java.io.*;

public class Th extends Thread
{
private int which;
private static int[] howMany = {0, 0, 0, 0} ;

static boolean[] end = {true,true};

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()
{
while(true)
{
synchronized(howMany)
{
if ( (howMany[0]+howMany[1]) < (howMany[2]+howMany[3]) &&
(howMany[0] <= 2*howMany[1]))
{
System.out.print("A");
howMany[0]++;
}
}
try
{
sleep(100);
}
catch(InterruptedException e) {}
}
}


public void ThB()
{
while(true)
{
synchronized(howMany)
{
if ( (howMany[0]+howMany[1]) < (howMany[2]+howMany[3]) )
{
System.out.print("B");
howMany[1]++;
}
}
try
{
sleep(3200);
}
catch(InterruptedException e) {}

}
}

public void ThC()
{
while(true)
{
synchronized(howMany)
{
System.out.print("C");
howMany[2]++;

try
{
howMany.wait();
}
catch(InterruptedException e) {}
}
try {
sleep(3100);
}
catch(InterruptedException e) {}
}
}

public void ThD()
{
while(true)
{
synchronized(howMany)
{
System.out.print("D");
howMany[3]++;
try {
howMany.notify();
}
catch(IllegalMonitorStateException e) {}
}
try {
sleep(1000);
}catch(InterruptedException e) {}
}
}

public static void main(String[] args)
{
Klawiatura k = new Klawiatura();
k.start();
}
}


class Klawiatura extends Thread
{
int n;

static void ustaw(int t)
{
switch (t)
{
case 0: Th.end[0] = false;
case 1: Th.end[1] = false;
case 2: {Th.end[0] = false;Th.end[1] = false;}

}
}

public void run()
{
BufferedReader i = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("podaj liczbe\n1 - Koniec A, B\n2 - Koniec C D\n3
- Koniec wszystkie: ");

Th Th1 = new Th(1);
Th Th2 = new Th(2);
Th Th3 = new Th(3);
Th Th4 = new Th(4);

Th1.start();
Th2.start();
Th3.start();
Th4.start();



while(true)
{
try
{
n = i.read();
System.out.println("N: "+n);
switch (n)
{
case 49:
{
System.out.println("Watki 1, 2: STOP");
Th1.interrupt();
}

case 50:
{
System.out.println("Watki 3, 4: STOP");
}

case 51:
{
System.out.println("Watki 1 -4: STOP");
}
}
}
catch(IOException e) {}

}
}
}
 
G

Gordon Beaton

I have 4 threads, they're writing on System.out in while(true) loop
one letter: A, B, C or D. A parent thread read from System.in and
according to input have to stop some of threads. But it doesn't work
- threads still printing letters. I don't know how to stop these
threads. can you help me?

Start by changing the loop condition in each thread from "while
(true)" to something that more closely expresses your intention.

Then have your parent thread do something that will change the value
of the improved loop condition.

/gordon
 
J

jimmie0

THANKS!! It worked:)
simply solutions are the best solutions.


Gordon Beaton napisal(a):
 
J

jimmie0

Last question: how to stop parent thread? i stopped all of 4 children
threads, but don't know how to stop parent... why my method doesn't
work?


import java.io.*;

public class Th extends Thread
{
private int which;
private static int[] howMany = {0, 0, 0, 0} ;

static boolean[] end = {true,true};

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()
{
while(end[0])
{
synchronized(howMany)
{
if ( (howMany[0]+howMany[1]) < (howMany[2]+howMany[3]) &&
(howMany[0] <= 2*howMany[1]))
{
System.out.print("A");
howMany[0]++;
}
}
try
{
sleep(50);
}
catch(InterruptedException e) {}
}
}


public void ThB()
{
while(end[0])
{
synchronized(howMany)
{
if ( (howMany[0]+howMany[1]) < (howMany[2]+howMany[3]) )
{
System.out.print("B");
howMany[1]++;
}
}
try
{
sleep(3200);
}
catch(InterruptedException e) {}

}
}

public void ThC()
{
while(end[1])
{
synchronized(howMany)
{
System.out.print("C");
howMany[2]++;

try
{
howMany.wait();
}
catch(InterruptedException e) {}
}
try {
sleep(100);
}
catch(InterruptedException e) {}
}
}

public void ThD()
{
while(end[1])
{
synchronized(howMany)
{
System.out.print("D");
howMany[3]++;
try {
howMany.notify();
}
catch(IllegalMonitorStateException e) {}
}
try {
sleep(1000);
}catch(InterruptedException e) {}
}
}

public static void main(String[] args)
{
Klawiatura k = new Klawiatura();
k.start();
}
}


class Klawiatura extends Thread
{
int n;
static boolean[] t = {true,true};

static void ustaw(int k)
{
switch (k)
{
case 0:
Th.end[0] = false;
t[0] = false;
break;
case 1:
Th.end[1] = false;
t[1]=false;
break;
case 2:
Th.end[0] = false;
Th.end[1] = false;
t[0]=false;
t[1]=false;
break;
}
}

public void run()
{
BufferedReader i = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("podaj liczbe\n1 - Koniec A, B\n2 - Koniec C D\n3
- Koniec wszystkie: ");

Th Th1 = new Th(1);
Th Th2 = new Th(2);
Th Th3 = new Th(3);
Th Th4 = new Th(4);

Th1.start();
Th2.start();
Th3.start();
Th4.start();

while(t[0] || t[1])
{
try
{
sleep(50);
n = i.read();
switch (n)
{
case 49:
{
ustaw(0);
System.out.println("\nWatki 1, 2: STOP");
break;
}

case 50:
{
ustaw(1);
System.out.println("\nWatki 3, 4: STOP");
break;
}

case 51:
{
System.out.println("\nWatki 1,2,3,4: STOP");
ustaw(0);
ustaw(1);
break;
}
}
}
catch(IOException e) {}
catch(InterruptedException e){}
for (int j=0;j<2;j++)
System.out.println("T["+j+"] = "+t[j]);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top