Java线程入门案例演示(一)

/**
 * 
 * 线程演示
 */
package cn.wang.test1;

public class demo1 {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
dog dg1 = new dog();
dog2 dg2 = new dog2();
Thread t1 = new Thread(dg1);
Thread t2 = new Thread(dg2);
t1.start();
t2.start();

}

}
class dog implements Runnable{
int n=0;
public void run(){
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
n++;
if(n==5) break;
System.out.println("dog1线程");
}
}
}

class dog2 implements Runnable{
int n=0;
public void run(){
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
n++;
if(n==5) break;
System.out.println("dog2线程");
}
}
}
posted @ 2016-11-28 13:56  To哥  阅读(74)  评论(0编辑  收藏  举报