Package com.user;
public class Thread_thread {
public static void Main (string[] args) {
TODO auto-generated Method Stub
MyThread m_t=new MyThread ();
M_t.setname ("thread");
M_t.start ();
Myrunnable myrunnable=new myrunnable ();
Thread m_r=new thread (myrunnable);
M_r.setname ("runnable");
M_r.start ();
System.out.println ("Thread and Runner");
}
Static class MyThread extends thread{
@Override
public void Run () {
TODO auto-generated Method Stub
Super.run ();
System.out.println (Thread.CurrentThread (). GetName () + "begins------");
int i = 1;
while (i<7) {
i++;
if (i%3==0) {
System.out.println (Thread.CurrentThread (). GetName () + "is working!!!");
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
System.out.println (Thread.CurrentThread (). GetName () + "ends======");
}
}
Static class Myrunnable implements runnable{
@Override
public void Run () {
TODO auto-generated Method Stub
System.out.println (Thread.CurrentThread (). GetName () + "begins------");
System.out.println (Thread.CurrentThread (). GetName () + "ends======");
}
}
}
Run the Java program:
A simple example of the thread and runnable in Java