Solved

Creating new threads from a static context

Posted on 2004-10-13
7
181 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is the compatible hibernate version for spring 4.3.1 10 64
scoresSpecial  challenge 13 42
endX challenge 2 50
topping2 challenge 13 61
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now