如何正确的创建Java线程池?Java多线程异步音频播放器

先看《阿里Java开发手册》线程池创建的规则:

一、编程规约

(六)并发处理

image.pngimage.png

 阿里的P3C开发规范插件会给出警告:

    private static ExecutorService executor = Executors.newFixedThreadPool(4);

image.png

 Java多线程异步音频播放器

实现Java音频播放器时,支持多线程异步播放,创建线程池时遵守《阿里Java开发手册》规约。

代码下载:https://github.com/rickding/HelloJava/tree/master/HelloAudio

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

class Player implements Runnable {
    private static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(
            4,
            new BasicThreadFactory.Builder().namingPattern("audio-player-pool-%d").daemon(true).build()
    );

    public static void asyncPlay(URL fileUrl) {
        if (fileUrl == null) {
            return;
        }

        // 播放进程
        Player player = new Player();
        try {
            player.audioStream = AudioSystem.getAudioInputStream(fileUrl);
        } catch (UnsupportedAudioFileException e) {
            System.err.println(e.getMessage());
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        executorService.execute(player);
    }

    public static void asyncPlay(ByteArrayOutputStream byteOutputStream) {
        if (byteOutputStream == null || byteOutputStream.size() <= 0) {
            return;
        }

        // 输入流
        byte[] audioBytes = byteOutputStream.toByteArray();
        int len = audioBytes.length;
        ByteArrayInputStream audioStream = new ByteArrayInputStream(audioBytes);
        AudioFormat audioFormat = FormatUtil.getAudioFormat();

        // 播放进程
        Player player = new Player();
        player.audioFormat = audioFormat;
        player.audioStream = new AudioInputStream(audioStream, audioFormat, len / audioFormat.getFrameSize());
        executorService.execute(player);
    }

    private AudioFormat audioFormat;
    private AudioInputStream audioStream;

    private Player() {
    }

    @Override
    public void run() {
        try {
            play(audioStream, audioFormat);
        } catch (LineUnavailableException e) {
            System.err.println(e.getMessage());
        } catch (IOException e) {
            System.err.println(e.getMessage());
        } finally {
            if (audioStream != null) {
                try {
                    audioStream.close();
                } catch (IOException e) {
                    System.err.println(e.getMessage());
                }
            }
        }
    }

    public static void play(AudioInputStream audioStream, AudioFormat audioFormat) throws IOException, LineUnavailableException {
        if (audioStream == null) {
            return;
        }

        if (audioFormat == null) {
            audioFormat = audioStream.getFormat();
        }

        DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
        SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine(lineInfo);
        dataLine.open(audioFormat, 1024);
        dataLine.start();

        byte[] bytes = new byte[1024];
        int len;
        while ((len = audioStream.read(bytes)) > 0) {
            dataLine.write(bytes, 0, len);
        }

        dataLine.drain();
        dataLine.close();
    }
}

猜你喜欢

转载自blog.51cto.com/13851865/2473871