Java学习笔记-5

线程

// DownFileTask.java
package com.company;

public class DownFileTask implements Runnable{
    @Override
    public void run() {
        System.out.println("Downloading file task " + Thread.currentThread().getName());
        try {
            // 中断线程约五秒
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Download complete " + Thread.currentThread().getName());
    }
}
// main.java
package com.company;

public class Main {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName());   // 打印线程的名字
        for(int i = 0; i < 10; i++) {
            // 创建新的线程
            Thread thread = new Thread(new DownFileTask());
            // 开始线程
            thread.start();
            // 类似于DownFileTask里的sleep, 在进程没有运行完之前阻塞线程
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("File is ready to be scanned");
        }
    }
}
// DownFileTask.java
package com.company;

public class DownFileTask implements Runnable{
    @Override
    public void run() {
        System.out.println("Downloading file task " + Thread.currentThread().getName());
        // 模拟下载文件,并取消文件下载
        for(int i = 0; i < Integer.MAX_VALUE; i++){
            if(Thread.currentThread().isInterrupted())
                return;
            System.out.println("Downloading byte " + i);
        }
        System.out.println("Download complete " + Thread.currentThread().getName());
    }
}

// main.java
package com.company;

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread(new DownFileTask());
        thread.start();
        try {
            thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 发送暂停线程的请求,但是不会主动停止请求
        thread.interrupt();
        System.out.println("File is ready to be scanned");
    }
}

并发问题

// DownloadStatus.java
package com.company;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DownloadStatus {
    private int totalBytes;
    private Lock lock = new ReentrantLock();

    public int getTotalBytes() {
        return totalBytes;
    }
    public void incrementTotalBytes(){
        // 线程锁
        lock.lock();
        try{
            totalBytes++;
        }finally {
            // 不论前面是否执行,一定要把锁解开
            lock.unlock();
        }

    }
}
// DownFileTask.java
package com.company;

public class DownFileTask implements Runnable{
    private  DownloadStatus status;

    public DownFileTask(DownloadStatus status){
        this.status = status;
    }
    @Override
    public void run() {
        System.out.println("Downloading file task " + Thread.currentThread().getName());
        for(int i = 0; i < 10_000; i++){
            if(Thread.currentThread().isInterrupted())
                return;
            status.incrementTotalBytes();
        }
        System.out.println("Download complete " + Thread.currentThread().getName());
    }
}
// Main.java
package com.company;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        DownloadStatus status = new DownloadStatus();

        List<Thread> threads = new ArrayList<>();
        for(int i = 0; i < 10; i++){
            Thread thread = new Thread(new DownFileTask(status));
            thread.start();
            threads.add(thread);
        }

        for(Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(status.getTotalBytes());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

horizonTel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值