Solved

Java and Threads

Posted on 2001-06-11
3
222 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ForLoop Example 3 52
Error in @AspectJ Based AOP with Spring 2 18
spring jars download 1 35
null output 3 35
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

860 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