Search code examples
javamultithreadingrunnable

How can I count how many times Thread has been suspended


I'm making a very simple program in java, even because I'm a student. I made a program that count 1 at 1000, and when the number is multiple for 2 the thread is suspended for about 500ms. That's ok. But in the final, I need to show how many times the thread has been suspended. Like

System.out.println("The thread has been suspended for: " );

I have no idea. I appreciate any help.

@Override
public void run () {
    for (int i = 0; i < 1000; i++) {
        if (i % 2 == 0) {
            try {
                System.out.println(i);
                Thread.sleep(500);

            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadNumeros.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    System.out.println("thread has been suspended for: ");
}

Solution

  • DO NOT MIX (SUSPENDED) WITH (SLEEP): they work differently.

    Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has acquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread.

    t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.

    stop(), suspend(), resume() are the methods used for thread implementation. stop() - terminate the thread execution, Once a thread is stopped, it cannot be restarted with the start() method, since stop() will terminate the execution of a thread. Instead you can pause the execution of a thread with the sleep() method. The thread will sleep for a certain period of time and then begin executing when the time limit is reached. But, this is not ideal if the thread needs to be started when a certain event occurs. In this case, the suspend() method allows a thread to temporarily cease executing. resume() method allows the suspended thread to start again.

    Synchronization-: If you declare any method as synchronized, it is known as a 'synchronized method'.

    A synchronized method is used to lock an object for any shared resource.

    When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.

    TRY LIKE THIS-:

    @Override
    public void run() {
    int count = 0;
        for (int i = 0; i < 1000; i++) {
            if (i % 2 == 0) {
                try {
                    System.out.println(i);
                    Thread.sleep(500);
                    count+=1
    
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadNumeros.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        System.out.println("thread has been suspended for: "+count);
    
    }