首页 > 代码库 > a simple example of thread and runnable in java
a simple example of thread and runnable in java
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 thread and runnable in java