bbalchev
bbalchev

Reputation: 867

Java and 2 threads

I am trying to learn Java's threads in order to do an assignment, but I do not understand how I can make each thread to do its own code. I also get an error:

Program.java:1: error: Program is not abstract and does not override abstract me
thod run() in Runnable
public class Program implements Runnable {
       ^
1 error

Because it is required by the assignment, I have to do everything within the same file, so I tried the code below:

public class Program implements Runnable {
    Thread thread1 = new Thread () {
        public void run () {
            System.out.println("test1");
        }
};

Thread thread2 = new Thread () {
    public void run () {
        System.out.println("test2"); 
        }
    };

    public void main (String[] args) {
        thread1.start();
        thread2.start();
    }
}

Could you please fix it for me and show how to have 2 threads which do different tasks from each other? I have already seen examples that print threads' names, but I did not find them helpful. Thank you.

Upvotes: 1

Views: 737

Answers (3)

jb10210
jb10210

Reputation: 1176

When you implement an interface (such as Runnable) you must implement its methods, in this case run.

Otherwise for your app to compile and run just erase the implements Runnable from your class declaration:

public class Program {
    public void main (String[] args) {
        Thread thread1 = new Thread () {
            public void run () {
                System.out.println("test1");
            }
        };    

        Thread thread2 = new Thread () {
            public void run () {
                System.out.println("test2");
            }
        };
        thread1.start();
        thread2.start();
    }
}

Upvotes: 0

Alex Stybaev
Alex Stybaev

Reputation: 4693

try this:

class Program {
    public static void main(String[] args) {
        Thread thread1 = new Thread() {
            @Override
            public void run() {
                System.out.println("test1");
            }
        };

        Thread thread2 = new Thread() {
            @Override
            public void run() {
                System.out.println("test2");
            }
        };
        thread1.start();
        thread2.start();
}

Or you can create a separate class implementing Runnable and ovverriding method run(). Then in main method create an instance of Thread with you class object as argument :

class SomeClass implements Runnable {
@Override
run(){
...
}
}

and in main:

Thread thread = new Thread(new SomeClass());

Upvotes: 0

Gray
Gray

Reputation: 116938

Your Program class is defined as implementing the Runnable interface. It therefore must override and implement the run() method:

public void run () {
}

Since your two Thread objects are using anonymous inner Runnable classes, you do not need and your should remove the implements Runnable from your Program class definition.

public class Program {
   ...

Upvotes: 3

Related Questions