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?