Java threads in and out
Posted on 12 Nisan 2015 in Programlama by admin
Following sample code shows how to implement basic runnable interface in java
// runnable interface interface MyRunnable { void run(); } // thread class in java class MyThread { MyRunnable r; String name; MyThread(MyRunnable run, String name) { this.r = run; this.name = name; } public static void sleep () { System.out.println("sleeping here"); } public void start() { System.out.println("Starting thread"); // executes run function using polymorphism // we are calling it in a blocking way however.. r.run(); } } class MyRunnableTest implements MyRunnable { MyRunnableTest() { MyThread t = new MyThread(this, "MyRunnableTest test"); t.start(); } public void run() { System.out.println("MyRunnableTest runs..."); MyThread.sleep(); } } public class deneme { public static void main(String prm[]) { MyRunnableTest p = new MyRunnableTest(); } }
and output is as follows:
Starting thread MyRunnableTest runs... sleeping here