Solved

Java and Threads

Posted on 2001-06-11
3
219 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now