Hey guys,

I've got an input stream from the keyboard and wanting to know how much time passes between when the user enters something to when the user next enters something. That makes sence?

Ive got a class called Timer

public class Counter implements Runnable {
    
    boolean startTimer = true;
    
    int m;
    int s;
    public Counter() {
        m=0;
        s=0;
    }
    
    public void run(){
        try {
            wait();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    //penguin
    public void resetTimer() {
        m=0;
        s=0;
    }
    
    public void startTimer() {
        while(startTimer) { try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        if(s==60) {
            s=0;
            m++;
        } else {
            s++;
        }
        System.out.println("TIME : " + m + ":" + s);
        }
    }
    
    public static void main(String args[]) {
        String a;
        
    }

The trouble is this ain't right. I'm not sure how to tell it to wait for first string before excuting. I need to do it with 2 threads ... any pointers?

Your intent is a little bit vague here. You need to wait for a string before actually doing anything and you also want to keep track of the time between two entries? In the first case, why not just wait for the input before starting the thread and for the second, variables that use calls to System.currentTimeMillis() should suffice.

It's hard to address those much more specifically without understanding more of the context.

Hey there cheers for the reply and the suggestions.

I've managed to knock up the following code whilst waiting for a reply..

package counter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Counter implements Runnable {
    
    Thread t = new Thread(this);
    boolean startTimer = true;
    
    volatile int m;
    volatile int s;
    
    public Counter() {
        m=0;
        s=0;
        t.start();
    }
    
    public void run(){
        startTimer();
    }
    
    
    public synchronized void resetTimer() {
        m=0;
        s=0;
        startTimer=false;
    }
    
    public synchronized void startTimer() {
        while(startTimer) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            if(s==60) {
                s=0;
                m++;
            } else {
                s++;
            }
            System.out.println("TIME : " + m + ":" + s);
        }
    }
    
    public static void main(String args[]) {
        String a;
        Counter c  = new Counter();
        
        String CurLine = ""; // Line read from standard in
        
        System.out.println("Enter a line of text (type 'quit' to exit): ");
        InputStreamReader converter = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(converter);
        
        
        while (!(CurLine.equals("quit"))){
            try {
                CurLine = in.readLine();
                c.resetTimer();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
            if (!(CurLine.equals("quit"))){
                System.out.println("You typed: " + CurLine);
            }
        }
    }
}

Sorry about not explaing i was making a nody program in order to implement something into a bigger project. Anyway the story goes im waiting for a stream of bytes to come from a socket. When that message as been processed it would reset the counter...

The question is how do i tell it to start counting when it recieves the message and not before.

Well, the design still seems a little bit questionable, but then I don't know the context of the larger project. Anyway, the issue you have above with the resetTimer() call not responding is probably due to bootstrapping the thread from within the Runnable and lock monitor contention on the synchronized methods.

Here is a slightly restructured version that does behave as you want.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Counter extends Thread {
    
    boolean timerRunning = false;
    
    volatile int s=0;
    
    public Counter() {
    }
    
    public void run(){
        timerRunning=true;
        startTimer();
    }
    
    public void resetTimer() {
        s=0;
    }
    
    public boolean timerRunning(){
        return timerRunning;
    }
    
    private void startTimer() {
        while(timerRunning) {
            try {
                Thread.sleep(1000);
                s++;
                System.out.println("TIME : " + (s/60) + ":" + (s%60));
            } catch (InterruptedException ex) {
                //ex.printStackTrace();
                timerRunning=false;
                Thread.currentThread().interrupt();
            }
        }
    }
    
    public static void main(String args[]) {
        Counter c = new Counter();
        

        String CurLine = ""; // Line read from standard in
        
        System.out.println("Enter a line of text (type 'quit' to exit): ");
        InputStreamReader converter = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(converter);
        
        while (!(CurLine.equals("quit"))){
            try {
                CurLine = in.readLine();
                if (c.timerRunning()){
                    c.resetTimer();
                } else {
                    c.start();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
            if (!(CurLine.equals("quit"))){
                System.out.println("You typed: " + CurLine);
            }
        }
        c.interrupt();
    }
}
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.