Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating new threads from a static context

Posted on 2004-10-13
7
Medium Priority
?
191 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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

876 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