Posted By:
vaibhav_belapurkar
Posted On:
Monday, February 19, 2001 10:14 AM
Respected Sir, I have some problem in Java Threading . In Java we can have a thread class that implements Runnable or we can extend it directly from thread class . I have win 98 and P-III processor I tried the code given below with both runnable and extends and I am getting different outputs. When I extend from thread only thread 1 executes till 9 and then goes in wait state. But when I use implements on thread1 both thread1 and thread2 start running and they run upto 4 and 5 respectively . Sir I tried it on another machine having win 98 and the result was same . If You could please help me in understand the difference in o/p it would be very helpful . Than
More>>
Respected Sir,
I have some problem in Java Threading . In Java we can
have a thread class that implements Runnable or we can
extend it directly from thread class . I have win 98 and
P-III processor I tried the code given below with both
runnable and extends and I am getting different outputs.
When I extend from thread only thread 1 executes till 9
and then goes in wait state. But when I use implements
on thread1 both thread1 and thread2 start running and they
run upto 4 and 5 respectively . Sir I tried it on another
machine having win 98 and the result was same . If You
could please help me in understand the difference in o/p
it would be very helpful .
Thanking You
Vaibhav Belapurkar
import java.lang.Object;
import java.lang.*;
class Stk{
static int sp = 9;
static int stk[] = new int[10];
synchronized void push(int no)
{
//String str = getName();
System.out.println("Inside push "+Prod.str1);
if(sp==0)
{
try{
//interrupt();
notify();
wait();
}
catch(Exception e)
{
System.out.println("Caught in push");
}
}
else
{
stk[sp] = no;
sp-=1;
//System.out.println("Inside else of push");
}
}
synchronized int pop()
{
int temp=0;
System.out.println("Inside pop");
if(sp==9)
{
try{
// notify();
//wait();
}
catch(Exception e)
{
System.out.println("Caught in pop");
}
}
else
{
temp = stk[sp];
sp+=1;
}
return temp;
}
}
class Thread1 implements Runnable
{
public void run()
{
Stk s1 = new Stk();
int i;
for(i=0;i
<14;i++)
{
System.out.println("Inside run " +i);
s1.push(i);
}
int temp = s1.pop();
System.out.println("Number Poped is "+temp);
}
}
class Prod
{
public static void main(String[]args)
{
Thread1 t1 = new Thread1();
Thread1 t2 = new Thread1();
Thread tt1 = new Thread(t1);
public String str1= tt1.setName("Thread1");
Thread tt2 = new Thread(t2);
public String str2= tt2.setName("Thread1");
System.out.println("Thread1 1111111");
tt1.start();
//t1.run();
System.out.println("Thread2 *******");
tt2.start();
//t2.run();
}
}
<<Less