Solved

Creating new threads from a static context

Posted on 2004-10-13
7
185 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 60 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Accepted Solution

by:
CEHJ earned 65 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 101
arguments to jar 5 35
ejb example issues 3 28
sql import cannot be resolved jsp 3 49
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

830 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