Search code examples
javamultithreadingextendrunnable

Java extends Thread not working but implements Runnable does. I don't see why?


I can't believe this is giving me trouble. My test on extend Thread does not work and my implement Runnable does. I have compared to working thread code I have written in the past and cannot see what's wrong.

Could someone point out my noob mistake?

Not working

public class PrimeThread extends Thread
{
    long minPrime;

    public PrimeThread(long minPrime)
    {
        this.minPrime = minPrime;
    }

    public static void main(String[] args) 
    {
        PrimeThread p = new PrimeThread(11);
        p.start();
    }

    public void Run()
    {
        System.out.println("wtf");
    }
}

Working

public class PrimeRun implements Runnable
{
    long minPrime;
    public PrimeRun(long in)
    {
        minPrime=in;
    }
    public static void main(String[] args) 
    {
        PrimeRun p = new PrimeRun(143);       
        new Thread(p).start();
    }

    @Override
    public void run()
    {
        System.out.println("hello");
    }
}

Solution

  • your "Run" method is capitalized. So it's a different method (i.e. you're not overriding "run")