Guys, is it legal to overload the run() method in a Thread class ?

It's legal to overload any method anywhere, but it probably won't do what you want. What you're supposed to do is override the run method. See here

What you're supposed to do almost certainly (there's hardly ever a need to derive from Thread) is to create a new class that implements Runnable and implement the run() method in that.

/* Ok...whats wrong in this code ? */


import java.io.*;

class Pat extends Thread
{
public static int cnt=0;
}
class Pattern1 extends Pat

{

    public void run()
    {
    for(int i=1;i<=8;i++)
    {
       System.out.print("|");
       cnt++;

        try
        {
         wait();
        }
        catch(InterruptedException e)
        {
        } if(cnt>i)
           resume();                    
    }
    }
} 

class Pattern2 extends Pat

{
    public void run()
    {
    for(int i=1;i<=7;i++)
    {
       System.out.print("*");
     try
        {
         wait();
        }
        catch(InterruptedException e)
        {
        }
    if(cnt>i+1)
    resume();
    cnt++;
    }

    }
} 

class Pattern

{
    public static void main(String args[])
    {
    Pattern1 p1=new Pattern1();
    Pattern2 p2=new Pattern2();

    p1.setPriority(10);
    p2.setPriority(8);

    p1.start();
    p2.start();

    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.