I the program I wish to run is a search for a number in 4 threads. Its working out fine and here is the code.

import java.lang.Math;
import java.lang.Thread;
public class NumberFinder
{
    public static void main(String args[])
    {
        int target = (int) (Math.random()*1000);
        System.out.println("The number is " + target);

        Thread thread0 = new Finder(target, 0, 249);
        Thread thread1 = new Finder(target, 250, 499);
        Thread thread2 = new Finder(target, 500, 749);
        Thread thread3 = new Finder(target, 750, 1000);
        thread0.start();
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Finder extends Thread
{

    int searchFor;
    int beginRange;
    int endRange;



    public Finder(int searchFor, int beginRange, int endRange)
    {
        this.searchFor = searchFor;
        this.beginRange = beginRange;
        this.endRange = endRange;
        System.out.println ("in constructor " + this.getName() + " " + beginRange + " " + endRange);
    }


    public void run()
    {
        for (int i = beginRange; i < endRange; i++){


            if (searchFor == i){

                System.out.println("found at " +this.getName()+ " and the number is "+  i);
            }

        }
    }
}

Now I wish to use Runnable instead of extends Thread

import java.lang.Math;
import java.lang.Thread;
public class NumberFinder
{
    public static void main(String args[])
    {
        int target = (int) (Math.random()*1000);
        System.out.println("The number is " + target);

        Thread thread0 = new Finder(target, 0, 249);
        Thread thread1 = new Finder(target, 250, 499);
        Thread thread2 = new Finder(target, 500, 749);
        Thread thread3 = new Finder(target, 750, 1000);
        thread0.start();
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Finder implements Runnable
{
     Thread t = Thread.currentThread();
    int searchFor;
    int beginRange;
    int endRange;



    public Finder(int searchFor, int beginRange, int endRange)
    {
        t.searchFor = searchFor;
        t.beginRange = beginRange;
        t.endRange = endRange;
        System.out.println ("in constructor " + t.getName() + " " + beginRange + " " + endRange);
    }


    public void run()
    {
        for (int i = beginRange; i < endRange; i++){


            if (searchFor == i){

                System.out.println("found at " +this.getName()+ " and the number is "+  i);
            }

        }
    }
}

But this code is not compiling. How to correct the code?

Since Finder is no longer a subclass of thread, you cannot store a Thread reference to it. Use Thread thread0 = new Thread(new Finder(target, 0, 249));

Since Finder is no longer a subclass of thread, you cannot store a Thread reference to it. Use Thread thread0 = new Thread(new Finder(target, 0, 249));

Thanks a lot! that fixed it.

import java.lang.Math;
import java.lang.Thread;
public class NumberFinder
{
    public static void main(String args[])
    {
        int target = (int) (Math.random()*1000);
        System.out.println("The number is " + target);
        Thread t = Thread.currentThread();
        Thread thread0 = new Thread(new Finder(target, 0, 250));
        Thread thread1 = new Thread(new Finder(target, 251,500 ));
	Thread thread2 = new Thread(new Finder(target,501, 750));
	Thread thread3 = new Thread(new Finder(target,751, 1000));
        thread0.start();
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Finder implements Runnable
{

    int searchFor;
    int beginRange;
    int endRange;



    public Finder(int searchFor, int beginRange, int endRange)
    {   Thread t = Thread.currentThread();
        t.searchFor = searchFor;
        t.beginRange = beginRange;
        t.endRange = endRange;
        System.out.println ("in constructor " + t.getName() + " " + beginRange + " " + endRange);
    }


    public void run()
    {
        for (int i = beginRange; i < endRange; i++){


            if (searchFor == i){

                System.out.println("found at " +t.getName()+ " and the number is "+  i);
            }

        }
    }
}
C:\Users\>javac NumberFinder.java
NumberFinder.java:32: cannot find symbol
symbol  : variable searchFor
location: class java.lang.Thread
        t.searchFor = searchFor;
         ^
NumberFinder.java:33: cannot find symbol
symbol  : variable beginRange
location: class java.lang.Thread
        t.beginRange = beginRange;
         ^
NumberFinder.java:34: cannot find symbol
symbol  : variable endRange
location: class java.lang.Thread
        t.endRange = endRange;
         ^
NumberFinder.java:46: cannot find symbol
symbol  : variable t
location: class Finder
                System.out.println("found at " +t.getName()+ " and the number is
 "+  i);
                                                ^
4 errors

Can somebody help me out here?

In method main write

Finder thread0 = new Finder(target, 0, 249);//4x change type of Thread->Finder

In constructor Finder 4x change

this.searchFor = searchFor; // change t. -> this.

Replace

Thread t = new Thread(this);//Thread.currentThread();

Write own method to start thread.

//

Of course kind of view presented by nmaillet is good too.

Use Thread thread0 = new Thread(new Finder(target, 0, 249));

In both cases you need

In constructor Finder 4x change
this.searchFor = searchFor; // change t. -> this.

Inside main method line

Thread t = Thread.currentThread();

is no needed.
Inside Finder constructor you don't need the line

Thread t = Thread.currentThread();

In both println methods use simply static metod

... + Thread.currentThread().getName() + ...

to reflect actual (not stored) name of thread.

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