-2

I have a Java class which already extends the Java default UnicastRemoteObject:

public class ApplicationServerImpl extends UnicastRemoteObject implements ApplicationServer

I wish to create a thread within this class to perform a check periodically using thread.sleep - could someone please show me how this is possible if I am already extending a default Java class.

TotalNewbie
  • 1,004
  • 5
  • 13
  • 25
  • http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html rarely, if ever, do you extend `Thread` – Brian Roach Feb 22 '14 at 23:43

1 Answers1

3

I'd take a look at some tutorials for threads.

http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

The tutorial shows you that you can fork a thread by implementing Runnable:

public class ApplicationServerImpl extends UnicastRemoteObject
    implements ApplicationServer, Runnable {
    ...

Then you can fork the thread like:

new Thread(new ApplicationServerImpl()).start();

Implementing Runnable is better because you can extend other classes.

You also should look into using the ExecutorService classes which take care of much of the thread work from you:

http://docs.oracle.com/javase/tutorial/essential/concurrency/exinter.html

Gray
  • 115,027
  • 24
  • 293
  • 354