Solved

Java and Threads

Posted on 2001-06-11
3
221 Views
Last Modified: 2010-04-16
As a newbie, I'm stumbling through the topic Java and threads and have come across a problem

Am ultimately trying to stop a thread from executing, although my issue is with Thread.currentThread().getName());

My code draws a GUI, from which a "Start" button is clicked and spawns a number of threads.  In the function that creates the threads, I have added a system out line with Thread.currentThread().getName());

The output is "Thread-2"


A separate button connecting to a different function, albeit containing the same system.out Thread.currentThread().getName()); line, gives me the following:-

"AWT-EventQueue-0"

I understand that this is a totally different thread.
However, how would I go about getting currentThread().getName() to give me the "Thread-2" and not "AWT-EventQueue-0" ?

Hope this all makes some sort of sense.

Andy
0
Comment
Question by:context
3 Comments
 
LVL 3

Accepted Solution

by:
superschlonz earned 50 total points
ID: 6179117
You cannot. The GUI Event is always processed by the AWT-EventQueue-0
Thread.
But you can communicat with other threads. There are several ways to do
that.
You could use a flag like this:

class MyThread extends Thread
{
  private boolean running = true;
  public void run()
  {
    while( running )
    {
      // do some work here
    }
  }
  public void stop()
  {
    running = false;
  }
}

class MyMain
{
  MyThread mythread;
  someFunc()
  {
    mythread = new MyThread();
    mythread.start;
  }
  anotherFunc()
  {
    mythread.stop();
  }
}

Another way is to use the interrupt() method of Thread:

class MyRunnable implements Runnable
{
  public void run()
  {
    // do some work
    if( Thread.currentThread().isInterrupted() )
    {
     return;
    }
    // more work ...
    if( Thread.currentThread().isInterrupted() )
    {
     return;
    }
   // more work ...
  }
}

class MyMain
{
  Thread thread;
  someFunc()
  {
    thread = new Thread( new MyRunnable() );
    thread.start;
  }
  anotherFunc()
  {
    thread.interrupt();
  }
}
0
 
LVL 4

Expert Comment

by:kylar
ID: 6179391
Please note that the stop() method of Thread has been deprecated because it is inherently unstable.  To expound on what super said:

Java spawns it's own threads. It spawns a main thread (when you start the program) and a thread that handles any interaction with the GUI. So if you click a button, that is processed by the AWT event thread (The AWT-EventQueue that you were getting.) What is happening is that the part of your code is being reached by 2 different threads. the first is by your thread, and the second by the Event Thread. If it is imperative that your code only be executed by a certain thread, you could put a check in like this:

Thread t = Thread.getCurrentThread();
if(SwingUtilities.isEventDispatchThread()){
//don't do anything.
}else{
 System.out.println(t.getName());
}
That way, your code will only be executed by your thread, and not the System Event Thread.

Cheers,
Kylar
0
 
LVL 92

Expert Comment

by:objects
ID: 6179536
If you call the function (with the System.out.println) from the function that creates your threads then you'll get the output is "Thread-2"  :-)

(But I don't think that's the answer you're really looking for).
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 85
eclipse shortcuts 9 54
use lov values 2 49
by zero exception 10 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question