简体   繁体   中英

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.

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 {
   ...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM