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

Recommended Answers

All 5 Replies

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

Yes you can!

commented: this added... nothing to the thread. -1

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();

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.