Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java and Threads

Posted on 2001-06-11
3
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
superschlonz earned 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

610 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