Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating new threads from a static context

Posted on 2004-10-13
7
Medium Priority
?
190 Views
Last Modified: 2010-03-31
Hi all,

My application has an abstract utility class which makes sounds.  It does this via static accessor methods like this:

public static void playError() {
  playSound(ERROR);
}

This is fine, but I want some of these methods to play the sound in a new thread, something like this:

public static void playClick() {
  playThreadSound(CLICK);
}

private static void playThreadSound( URL CLICK) {
  // ---- This won't work, trying to create instance from a static context!
  SoundThread thread = new SoundThread(CLICK);    
  thread.start();
}

Can anyone show me how to get the functionality I require, without having to create an instance of my Utility class?  Is what I'm trying to do sensible?
0
Comment
Question by:agrees
[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
7 Comments
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 240 total points
ID: 12297567
Make your Utiltity class like:

public class Utility {
   private static Utility theInstance = null;

   private static Utility getInstance() {
       if (theInstance==null)
          theInstance = new Utility();
       return theInstance;
   }
 
   public static void playError() {
         playSound(ERROR);
   }
}

now from a static context you can call:

     Utility.getInstance().playError();
0
 

Author Comment

by:agrees
ID: 12298043
Bingo, just what I was after, thanks zzynx!

I'll give it a bit just in case anyone wants to argue a better way, then give you the points! :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12299350
OK :)
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 86

Accepted Solution

by:
CEHJ earned 260 total points
ID: 12302024
Just create a Thread in one of your static methods:


public static void playThreadSound(final URL click) {
      Thread playerThread = new Thread(new Runnable() {
            public void run() {
                  playSound(click);
      
            }    
      });
      playerThread.start();
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12303601
>   // ---- This won't work, trying to create instance from a static context!

you can create an instance from a static context (unless it is an inner class).
If it is an inner class then either move it from being an inner class or make it static (the class that is).

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12318552
Thank you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12322758
8-)
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

688 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