Solved

Java and Threads

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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