what does an non parameterized constructor of thread do and how can its sleep method be called before starting it

Viewed 46
import java.io.*;
import java.util.*;

class queue
{
    int q[],f=0,r=0,size;
    void insert(int n ){
        Scanner in =new Scanner(System.in);
        q=new int[10];
        for(int i=0;i<n;i++){
            System.out.println("\n enter"+i+"element");
            int ele= in.nextInt();
            if(r+1>10){
                System.out.println("\n queue is full\n lost packets"+ele);
                break;
            }
            else{
                r++;
                q[i]=ele;
            }
        }
    }

    void delete(){
        Scanner in = new Scanner(System.in);
        Thread t = new Thread();
        if(r==0)
            System.out.println("\n Queue is empty");
         else{
             for(int i=f;i<r;i++){
                 try{
                      t.sleep(1000);
                  }
                  catch(Exception e){}
                  System.out.println("\n leaked packet"+q[i]);
                   f++;
                  }
              }
              System.out.println();
         }
 }

            public class Leaky extends Thread {

                public static void main(String[] args) throws Exception {
                    queue q = new queue();
                    Scanner src = new Scanner(System.in);
                    System.out.println("enter the packet to be sent");
                    int size = src.nextInt();
                    q.insert(size);
                    q.delete();

                }

            }

This is code which is to demonstrate leaky bucket algorithm in networks. Here, how can the thread's sleep method be called before it is started? Also this code gives output on every second. How is this possible when threads run parallel with main thread? How can that affect the output(ie introduce a delay of 1second)? Where is non parameterized constructor used, how can the run method be called internally by jvm when we don't pass the class which either extends Thread class or implements Runnable interface as a parameter?

1 Answers

Here,how can the thread's sleep method be called before it is started.

Thread.sleep is a static method, which Causes the currently executing thread to sleep. i.e. this code causes the main thread to sleep. It doesn't matter if you call it with t.sleep(1000) or Thread.sleep(1000) - the result is the same.

The Thread instance created in this code (by Thread t = new Thread()) is never started, so there is only one thread running.

Also this code gives output on every second. How is this possible when threads run parallel with main thread.How can that affect the output(ie introduce a delay of 1second).

As I said, there is only one thread running here - the main thread. Thread.sleep(1000) causes that thread to sleep for 1 second.

Where is non parameterized constructor used,how can the run method be called internally by jvm when we don't pass the class which either extends Thread class or implements Runnable interface as a parameter

If you add a call to t.start() in this code, it will execute Thread's run() method, which will do nothing, as the Javadoc says - If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

Creating a Thread with the parameter-less constructor is not useful (since that thread will do nothing). However, if you create a sub-class of Thread that overrides the run() method, your sub-class's constructor can call Thread's parameter-less constructor, so that constructor is still useful.

Related