Java threads in and out

Posted on 12 Nisan 2015 in Programlama by

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

Please give us your valuable comment

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image

This site uses Akismet to reduce spam. Learn how your comment data is processed.