Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,369 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,436 people online right now. Registration is fast and FREE... Join Now!




Thread suspention

 

Thread suspention

Neosmithk

2 Jul, 2009 - 09:19 PM
Post #1

D.I.C Head
**

Joined: 24 Jun, 2009
Posts: 55



Thanked: 4 times
My Contributions
Hi, i was trying another example form the java a beginners guide, and the way the code was written its putting a flag on this one part of the code (marked). if i try to take it out it doesn't display the way it should - it doesn't switch between the Main thread and the child thread. i was wondering if anyone had any ideas.

CODE

package booksuspend11;

class MyThread implements Runnable
{
    Thread thrd;

    volatile boolean suspended;
    volatile boolean stopped;

    MyThread(String name)
    {
        thrd = new Thread(this, name);
        suspended = false;
        stopped = false;
        thrd.run();
    }

    public void run()
    {
        System.out.println(thrd.getName() + " is starting.");
        try
        {
            for(int i = 1; i < 1000; i++)
            {
                System.out.println(i + " ");
                if((i%10) == 0)
                {
                    System.out.println();
                    Thread.sleep(250);
                }
            }

            synchronized(this)
            {
                while(suspended)
                {
                    wait();
                    
                }
                if(stopped) break;  // ****ERR: Break outside switch or loop.****
            }
        }
        catch(InterruptedException exc)
        {
            System.out.println(thrd.getName() + " interrupted.");
        }
        System.out.println(thrd.getName() + " exiting.");
    }

    synchronized void mystop()
    {
        stopped = true;
        suspended = false;
        notify();
    }

    synchronized void mysuspend()
    {
        suspended = true;
    }

    synchronized void myresume()
    {
        suspended = false;
        notify();
    }
}
public class Suspend
{

    public static void main(String[] args)
    {
        MyThread obl = new MyThread("MyThread");

        try
        {
            Thread.sleep(1000);

            obl.mysuspend();
            System.out.println("Suspending thread.");
            Thread.sleep(1000);

            obl.myresume();
            System.out.println("Resuming thread.");
            Thread.sleep(1000);


            obl.mysuspend();
            System.out.println("Suspending thread.");
            Thread.sleep(1000);

            obl.myresume();
            System.out.println("Resuming thread.");
            Thread.sleep(1000);

            obl.mysuspend();
            System.out.println("Stopping thread.");
            obl.mystop();
        }
        catch(InterruptedException exc)
        {
            System.out.println("Main thread interrupted.");
        }

        try
        {
            obl.thrd.join();
        }
        catch(InterruptedException exc)
        {
            System.out.println("main thread interrupted.");
        }
        System.out.println("Main thread exiting.");
    }
    
}


User is offlineProfile CardPM
+Quote Post


scrat

RE: Thread Suspention

3 Jul, 2009 - 04:02 AM
Post #2

New D.I.C Head
*

Joined: 30 Jun, 2009
Posts: 18



Thanked: 7 times
My Contributions
Uhm, I've spend some time playing with this code now. My suggestion would be to forget this one and Google again for a better example. It's a mess and I doubt there is much to be learned from it.

http://www.javaworld.com/javaworld/jw-04-1...04-threads.html
User is offlineProfile CardPM
+Quote Post

pbl

RE: Thread Suspention

3 Jul, 2009 - 12:57 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,535



Thanked: 1125 times
Dream Kudos: 450
My Contributions
An actually you are not running a Thread (despite your 8 years of browsing and 3 years of programming experience :-))

CODE

    MyThread(String name)
    {
        thrd = new Thread(this, name);
        suspended = false;
        stopped = false;
        thrd.run();   // <----- this just calls your run method as any other method
    }


you have to:

CODE

    MyTread mt = new MyTread();
    Thread t = new Thread(mt);
    t.start();


in your case, as your class MyThread does not extends any other class even better to make it to extend thread rather than implementing Runnable

CODE

class MyThread extends Thread {
...
then in Suspend
...
MyThread t = new MyThread();
t.start();


and do not forget to remove: thrd.run(); from your constructor

This post has been edited by pbl: 3 Jul, 2009 - 08:53 PM
User is online!Profile CardPM
+Quote Post

Dantheman

RE: Thread Suspention

3 Jul, 2009 - 01:02 PM
Post #4

D.I.C Regular
***

Joined: 27 May, 2009
Posts: 445



Thanked: 25 times
My Contributions
Indeed. Calling a run() method manually does what you would expect any other method to do - you're simply invoking it's body inside the current thread.


User is offlineProfile CardPM
+Quote Post

Neosmithk

RE: Thread Suspention

4 Jul, 2009 - 09:25 PM
Post #5

D.I.C Head
**

Joined: 24 Jun, 2009
Posts: 55



Thanked: 4 times
My Contributions
sorry, i was out for a while, i made a typo in that code it was supposed to be thrd.start(); not thrd.run(); sorry about that. the problem remains the same though...
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 09:02PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month