1.
package zuoye1;
/**
* @author Oldhu
* @date @Date 2020/8/5
* @des
*/
public class TestThread extends Thread {
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
System.out.println((i+1)+",你好,来自线程"+Thread.currentThread().getName());
}
}
public static void main(String[] args) {
TestThread t=new TestThread();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.setName("Thread-0");
t2.setName("Thread-1");
t1.start();
t2.start();
}
}
2.
package zuoye2;
/**
* @author Oldhu
* @date @Date 2020/8/5
* @des
*/
public class TestRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
System.out.println((i+1)+",你好,来自线程"+Thread.currentThread().getName());
}
}
public static void main(String[] args) {
TestRunnable t=new TestRunnable();
Thread t1=new Thread(t,"Thread-0");
Thread t2=new Thread(t,"Thread-1");
t1.start();
t2.start();
}
}
3.
package zuoye3;
/**
* @author Oldhu
* @date @Date 2020/8/5
* @des
*/
public class ClimbThread extends Thread{
private int time;
private int num;
public ClimbThread(String name,int time, int num) {
super(name);
this.time = time;
this.num = num;
}
public void run(){
while(num>0){
try {
Thread.sleep(this.time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"爬了100米!");
num--;
}
System.out.println(Thread.currentThread().getName()+"到了终点");
}
public static void main(String[] args) {
ClimbThread ct=new ClimbThread("年轻人",100,10);
ClimbThread ct2=new ClimbThread("老年人",150,10);
ct.start(