cartarget.java
public class cartarget implements Runnable{
public void run()
{
for(int i=1;i<=20;i++)
{
System.out.print("汽车"+i+" ");
}
}
}
elephanttarget.java
public class elephanttarget implements Runnable{
public void run()
{
for(int i=1;i<=20;i++)
{
System.out.print("大象"+i+" ");
}
}
}
example2.java
public class example2{
public static void main(String[] args)
{
Thread speakelephant;
Thread speakcar;//声明线程
elephanttarget elephant;
cartarget car;//目标对象
elephant=new elephanttarget();
car=new cartarget();
speakelephant=new Thread(elephant);
speakcar=new Thread(car);
speakelephant.start();
speakcar.start();
for(int i=1;i<=15;i++)
{
System.out.print("主人"+i+" ");
}
}
}