20200805多线程习题(多线程计算素数个数,多线程实现多个文件同步复制功能)

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(
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值