Java基础练习(十六)多线程简单创建

1、使用多线程,模拟龟兔赛跑的场景。
// -- 小乌龟
public class Gui implements  Runnable {
    @Override
    public void run() {
        Thread.currentThread().setName("乌龟:");
        for (int i = 0; i <1000 ; i++) {
            System.out.println(Thread.currentThread().getName()+i);
        }
    }
}

//-- 兔子
public class Tu implements  Runnable{
    public void run() {
        Thread.currentThread().setName("兔子:");//直接调用类方法
        for (int i = 0; i <1000 ; i++) {
            System.out.println(Thread.currentThread().getName()+i);
        }
        try {
            //兔子睡觉
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

//-- 测试
public class Test {
    public static void main(String[] args) {

        Gui gui = new Gui();
        Tu tu = new Tu();

        Thread thread = new Thread(gui);
        Thread thread1 = new Thread(tu);

        thread.start();
        thread1.start();
        System.out.println("龟兔赛跑开始");
    }
}

2、编写一个有两个线程的程序,第一个线程用来计算2~100000之间的奇数的个数,第二个线程用来计算100000~200000之间的偶数的个数,最后输出结果。
//-- 奇数
public class Threadone extends Thread {
    @Override
    public void run() {
        super.run();

        int sum=0;

        for (int i = 2; i <100000 ; i++) {
            if (i%2!=0){
                sum++;
            }
        }
        System.out.println("2-100000中的奇数个数为:"+sum);
    }
}

//-- 偶数
public class Threadtwo extends Thread {
    @Override
    public void run() {
        super.run();

        int sum=0;

        for (int i = 100000; i <200000 ; i++) {
            if (i%2==0){
                sum++;
            }
        }
        System.out.println("1000000-200000中的偶数个数为:"+sum);
    }
}
//-- 测试
public class Test {
    //主线程
    public static void main(String[] args) {
        Threadone threadone = new Threadone();
        Threadtwo threadtwo = new Threadtwo();

        threadone.start();
        threadtwo.start();

    }
}
3、使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
import java.io.*;
import java.math.BigDecimal;

public class MyThread extends Thread {

     private File oldfile;
     private File newfile;

    public MyThread(String oldfilepath, String newfilepath) {
        this.oldfile = new File(oldfilepath);
        this.newfile = new File(newfilepath);
    }

    @Override
    public void run() {
        super.run();
        FileInputStream fileInputStream =null;
        FileOutputStream fileOutputStream =null;

        try {
            fileInputStream= new FileInputStream(oldfile);
            fileOutputStream= new FileOutputStream(newfile);

            byte[] bytes = new byte[1024];
            int read = fileInputStream.read(bytes);
            //获取文件长度
           double jdt=(double)oldfile.length();
            
            int jd=0;
            while (read!=-1){
                 jd=jd+read;
                BigDecimal divide =
                        new BigDecimal(jd).divide(new BigDecimal(jdt), 2, BigDecimal.ROUND_HALF_UP);
                System.out.println(oldfile.getName()+"复制进度"+divide.multiply(new BigDecimal(100))+"%");
                fileOutputStream.write(bytes,0,read);
                read = fileInputStream.read(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //任何情况下都关闭流
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

//-- 测试
import java.io.IOException;

public class MainThreadTest {
    public static void main(String[] args) throws IOException {
        MyThread myThread = new MyThread("src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp1\\3.jpg", "src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp2\\3.jpg");
        MyThread myThread1 = new MyThread("src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp1\\4.jpg", "src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp2\\4.jpg");

        myThread.start();
        myThread1.start();
    }
}
4、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。不考虑线程的安全性。
// --共享j
public class J {
    public static int j=0;
}
// -- 加线程
public class Ixc1 extends  Thread{
    @Override
    public void run() {
        super.run();
        for (int i = 0; i < 100; i++) {
            System.out.println("加:"+J.j++);
        }
    }
}
//-- 减线程
public class Xc1 extends  Thread {
    @Override
    public void run() {
        super.run();
        for (int i = 0; i <100 ; i++) {
            System.out.println("减:"+J.j--);
        }
    }
}
// --测试
public class Test {
    public static void main(String[] args) {
        Ixc1 ixc1 = new Ixc1();
        Ixc1 ixc11 = new Ixc1();

        Xc1 xc1 = new Xc1();
        Xc1 xc11 = new Xc1();

        ixc1.start();
        ixc11.start();
        xc1.start();
        xc11.start();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值