后端小树林

并发知识5

2018-08-04  本文已影响0人  奔跑的蛙牛

死锁

锁和条件不能解决线程中的所有问题
账户1:200; 账户2:300;
线程1:从账户1转移300到账户2 线程2: 从账户2转移400到账户1
线程一和线程二都被阻塞了
两个账户的余额都不足以转账,两个线程无法执行下去
这样的状态称为死锁

线程局部变量

public static final SimpleDateFormate
dateFormdate = new SimpleDateForniat("yyyy-MM-dd");

String dateStamp = dateFormat.get() .format(new Date()) ; //调用

锁测试与超时

调用lock方法获取其他线程的锁可能会发生阻塞,应该更加安全的申请锁

    if(myLock.tryLock()){
        try{
        }finnally{
          myLock.unlock();
        }
    }else{
        // do something else
    }
  //  设置超时
  myLock.tryLock(100,TineUnit.MILLISECONDS)

弃用的方法

stop 方法不安全,强制停止线程
suspend 方法可能导致一个拥有锁的线程被挂起

阻塞队列

线程队列:使用队列以优雅且安全的方式将其形式化

package blockQueue;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockQueueTest {
    public static final int FILE_QUEUE_SIZE = 10;
    public static final int SEARCH_THREADS = 100;
    private static final File DUMMY = new File("");
    private static BlockingQueue<File> queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE);

    public static void main(String[] args){
        try (Scanner in = new Scanner(System.in)){
            System.out.println("Enter base direcory(...)");
            String direcory = in.nextLine();
            System.out.println("Enter keywords");
            String keyword = in.nextLine();
            Runnable enumerator = () ->{
                try {
                    enumerate(new File(direcory));
                    queue.put(DUMMY);

                }catch (InterruptedException e){

                }
            };
            new Thread(enumerator).start();
            for (int i = 1; i <=SEARCH_THREADS ; i++) {
                Runnable search = ()->{
                    try {
                        boolean done = false;
                        while (!done){
                            File file = queue.take();
                            System.out.println(file);
                            if(file == DUMMY){
                                queue.put(file);
                                done = true;
                            }else search(file,keyword);
                        }
                    }catch (IOException e){
                        e.printStackTrace();
                    }catch (InterruptedException e){

                    }
                };
                new Thread(search)
                        .start();

            }

        }
    }
    public static void enumerate(File directory) throws InterruptedException{
        File[] files = directory.listFiles();
        for (File file:
             files) {
            if(file.isDirectory()) enumerate(file);
            else {
                System.out.println(file.getName());
                queue.put(file);
            }
        }
    }
    public static void search(File file,String keywords)throws IOException{
        try(Scanner in = new Scanner(file,"UTF-8")) {
            int lineNumber = 0;
            while (in.hasNextLine()){
                lineNumber++;
                String line = in.nextLine();
                if(line.contains(keywords)){
                    System.out.printf("%s;%d;%s%n", file.getPath(), lineNumber, line);
                }
            }
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读