Solved

Creating new threads from a static context

Posted on 2004-10-13
7
188 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

630 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