Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8200131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:19:58+00:00 2026-06-07T06:19:58+00:00

Eg. – take the class import java.awt.Color; import java.util.Random; import javax.swing.JLabel; public class flashThread

  • 0

Eg. – take the class

import java.awt.Color;
import java.util.Random;

import javax.swing.JLabel;

public class flashThread implements Runnable {
private JLabel temp;
Thread thread;
Color randColor;

public flashThread(JLabel toFlash) {
    temp = toFlash;
    thread = new Thread(this);
}

public void run() {
    Random r = new Random();
    while (true) {
        temp.setForeground(new Color(r.nextInt(246) + 10,
                r.nextInt(246) + 10, r.nextInt(246) + 10));
        String tempString = temp.getText();
        temp.setText("");
        try {
            thread.sleep(r.nextInt(500));
        } catch (InterruptedException e) {
        }
        temp.setText(tempString);
        try {
            thread.sleep(r.nextInt(500));
        } catch (InterruptedException e) {
        }
    }
}

public void begin() {
    thread.start();
}

}

If I added thread.start() in the constructor, when I create two objects of the flashThread, Only one of them flashes. However, If i remove then, add the begin() method and then call the begin method from the class that initialized both flashThread objects, they both flash.

Any Help – I’ve only just started to learn about threads.

Thanks in advance!
-Me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:20:00+00:00Added an answer on June 7, 2026 at 6:20 am

    First of all please start your class name with a capital letter since this is convention in java.
    It doesn’t matter if you start your thread in the constructor or in a method. One problem is that you access UI elements outside of the Event Dispatching Thread (EDT), which is the only one which is allowed to access, UI elements.
    Use SwingUtilities.invokeLater instead. Furthermore invoke static methods like Thread#sleep on classes e.g. Thread.sleep(1000); not on instances.
    So updating your code would look like:

    public class FlashThread implements Runnable {
      private JLabel temp;
      Thread thread;
      Color randColor;
    
      public FlashThread(JLabel toFlash) {
        temp = toFlash;
        thread = new Thread(this);
        thread.start();
      }
    
      public void run() {
        final Random r = new Random();
        while (true) {
            SwingUtilities.invokeAndWait(new Runnable(){
               public void run() {
                  // this will be executed in the EDT
                  temp.setForeground(new Color(r.nextInt(246) + 10,
                    r.nextInt(246) + 10, r.nextInt(246) + 10));
                  // don't perform long running tasks in the EDT or sleep
                  // this would lead to non-responding user interfaces
               }
            });
            // Let our create thread sleep
            try {
                Thread.sleep(r.nextInt(500));
            } catch (InterruptedException e) {
            }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to craft a Java regular expression to split strings of the general
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I would like to count the length of a string with PHP. The string
I've got the following login script.. <?php $name = $_POST[name]; $password = $_POST[password]; $query
So to start, I have an array of XML files. These files need to
I'm struggling to position the second level of a dropdown nav menu and was
Have a webpage that will be viewed by mainly IE users, so CSS3 is
Welcome to Spring Roo. For assistance press CTRL+SPACE or type hint then hit ENTER.
I am reading some og tags from sites but I can't seem to decode
I have a simple restful service that transforms a JAXB-anntotated beans to response XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.