JAVA--实现线程
/*
线程两种方法中的第一种方法
class XianCheng
{
public static void main(String[] args)
{
Cat dog=new Cat();
dog.start();
}
}
class Cat extends Thread
{
public void run()
{
int times=0;
while(true)
{
//休眠一秒,线程一遇到sleep就会进入到blocked状态,并释放资源
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
times++;
System.out.println("this is my test "+ times);
if (times==10)
{
break;
}
}
}
}
*/
//线程两种方法中的第二种方法
public class XianCheng
{
public static void main(String[] args)
{
Cat dog=new Cat();
Thread t=new Thread(dog);
t.start();
}
}
class Cat implements Runnable
{
int times=0;
public void run()
{
while(true)
{
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
times++;
System.out.println("this is a test "+times);
if (times==10)
{
break;
}
}
}
}