Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Threads interruption

Thread view: 
jimmie0@wp.pl - 21 Nov 2006 12:17 GMT
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)
            {}
        }
    }
}
jimmie0@wp.pl - 21 Nov 2006 12:57 GMT
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) {}
               
        }
    }
}
Gordon Beaton - 21 Nov 2006 13:18 GMT
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

jimmie0@wp.pl - 21 Nov 2006 13:36 GMT
THANKS!! It worked:)
simply solutions are the best solutions.

Gordon Beaton napisal(a):
> > 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
[quoted text clipped - 13 lines]
> [ don't email me support questions or followups ]
> g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e
jimmie0@wp.pl - 21 Nov 2006 13:55 GMT
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]);
        }
    }
}
rxreyn3@gmail.com - 25 Nov 2006 06:36 GMT
There is a very powerful library available to handle concurrency.
Recently alot of it has even been included in java 5.  You may want to
look at it:

http://g.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

Ryan

> Hello.
> I have 4 threads, they're writing on System.out in while(true) loop one
[quoted text clipped - 53 lines]
>     }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.