Hi. I'm trying to learn about using threads in java, so I decided to try to make a new thread in which an alarm rings while the rest of the program continues.

My program is supposed to go about it's business graphing some data, and then if the data exceeds a certain value it trips an alarm.

When I compile I get this error
So when I compile It says:

cannot resolve symbol
symbol  : method start ()
location: class Alarm
            alert.start();//audible warning

Then in the main part of the program where I actually use the alarm is this.

Alarm alert;

alert = new Alarm();

//... more code

if (tripped){
   //...do stuff
   alert.start();
}

Here is my alarm class:

import  sun.audio.*;  
import  java.io.*;   
import java.awt.*;
import java.lang.*;

public class Alarm implements Runnable{
    Thread t;
    InputStream inAudio;
    AudioStream alert1;
    
    public void init(){
        t = new Thread(this);
        t.start();
        
        inAudio = getClass( ).getResourceAsStream("alert1.wav");
        try{
            alert1 = new AudioStream(inAudio);
        }
        catch(IOException ex){
            System.out.println("could not declare alert as a new AudioStream");
        }
    }
    
    public void run(){
        while (true){
            
            AudioPlayer.player.start(alert1);
            
            try{
                
                t.sleep(500);
            }
            catch (InterruptedException ex){}
        }
    }
    
    public void stop(){
        t.stop();
    }
 }

Could someone please help me understand what I am doing wrong?
Thanks

Recommended Answers

All 3 Replies

Alarm a = new Alarm();
Thread t = new Thread(a);
t.start();

Alarm implements runnable, that means it must define a method run, but it is not, in itself a Thread object. The method start is a method of Thread. You create a new Thread with your Alarm object by creating an instance of Thread including in the "new" call an instance of your Alarm class (i.e. a Runnable object). Then you call start on the thread, which in turn, executes your run method.

If you want to be able to call a.start(), then you need to make Alarm extend Thread rather than implement Runnable.

I think you need to reread the Thread tutorials.

Thanks, got it now with your explanation.

Thanks for helping earlier with my other question as well.

Welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.