多线程经典练习题

2023-05-16

线程练习题

1、用共享资源的方式实现 生产者–仓库–消费者的模式(注意线程安全)
线程通信

public class Test4 {
    public static void main(String[] args) {
        Storage s=new Storage();
        Worker w=new Worker();
        w.setName("生产者");
        w.s=s;
        Customer c=new Customer();
        c.s=s;
        c.setName("消费者");
        w.start();
        c.start();
    }
}
class Worker extends Thread{
    Storage s;
    public void run(){
        while (true){
            s.addStore();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Customer extends Thread{
    Storage s;
    public void run(){
        while (true) {
            s.minusStore();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Storage{
    public int stores=10;
    public synchronized void addStore(){
        if (stores>=20){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            stores++;
            System.out.println(Thread.currentThread().getName()+"生产产品,当前库存为:"+stores);
            notifyAll();
        }
    }

    public synchronized void minusStore(){
        if (stores<=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            stores--;
            System.out.println(Thread.currentThread().getName()+"消费者进行消费,当前库存为:"+stores);
            notifyAll();
        }
    }
}

2、使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
PS:DecimalFormat中的format方法可以将小数转成百分数

public class Test3 extends Thread {
    private File file;
    private File target;
    private int len;//每次读取的长度

    public Test3(File file, File target,int len) {
        this.file = file;
        this.target = target;
        this.len=len;
    }
    
    @Override
    public void run() {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream(file);
            fos=new FileOutputStream(target);
            byte[] b=new byte[len];
            int tmp;
            int total=fis.available();
            double loaded=0;
            while ((tmp=fis.read(b))!=-1){
                fos.write(tmp);
                loaded+=tmp;
                double per=loaded/total;
//                double percent=(int)(per*10000)/100.0;
//                System.out.println(file.getName()+"已读取"+percent+"%");
                DecimalFormat df=new DecimalFormat("##.##%");
                System.out.println(file.getName()+"已读取"+df.format(per));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Test3 t1=new Test3(new File("E:/work/1.txt"),new File("E:/PlayCards/1.txt"),1);
        Test3 t2=new Test3(new File("E:/work/2.txt"),new File("E:/PlayCards/2.txt"),1);
        Test3 t3=new Test3(new File("E:/work/3.txt"),new File("E:/PlayCards/3.txt"),1);
        t1.start();
        t2.start();
        t3.start();
    }
}

3、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。考虑到线程的安全性写出程序

public synchronized void run() {
        int j=0;
        if (Thread.currentThread().getName().equals("线程1") || Thread.currentThread().getName().equals("线程2")){
            j++;
        }
        if (Thread.currentThread().getName().equals("线程3") || Thread.currentThread().getName().equals("线程4")){
            j--;
        }
        System.out.println(Thread.currentThread().getName()+":"+j);
    }

public static void main(String[] args) {
        HomeWork t=new HomeWork();
        Thread t1=new Thread(t,"线程1");
        Thread t2=new Thread(t,"线程2");
        Thread t3=new Thread(t,"线程3");
        Thread t4=new Thread(t,"线程4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

4、编写一个有两个线程的程序,第一个线程用来计算2~100000之间的素数的个数,第二个线程用来计算100000~200000之间的素数的个数,最后输出结果

public void run() {
        for (int i = 100000; i <= 200000; i++) {
            boolean is=true;
            for (int j = 2; j < i; j++) {
                if (i%j==0){
                    is=false;
                    break;
                }
            }
            if (is)count++;
        }
        System.out.println(Thread.currentThread().getName()+":100000到200000之间有"+count+"个素数");
    }

public void run() {
        for (int i = 2; i <= 100000; i++) {
            boolean is=true;
            for (int j = 2; j < i; j++) {
                if (i%j==0){
                    is=false;
                    break;
                }
            }
            if (is)count++;
        }
        System.out.println(Thread.currentThread().getName()+":2到100000之间有"+count+"个素数");
    }

public static void main(String[] args) {
        Abc r=new Abc();
        Bbb r2=new Bbb();
        Thread t1=new Thread(r,"线程1");
        Thread t2=new Thread(r2,"线程2");
        t1.start();
        t2.start();
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

多线程经典练习题 的相关文章

随机推荐