生产者消费者模式用队列实现,如何改写为长尾?

2026-04-15 10:0811阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计320个文字,预计阅读时间需要2分钟。

生产者消费者模式用队列实现,如何改写为长尾?

javapublic class Consumer implements Runnable { private final BlockingQueue sharedQueue;

public Consumer(BlockingQueue sharedQueue) { this.sharedQueue=sharedQueue; }

生产者消费者模式用队列实现,如何改写为长尾?

public void run() { while (true) { try { sharedQueue.take(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } }}

Consumer.java

public class Consumer implements Runnable { private final BlockingQueue sharedQueue; public Consumer(BlockingQueue sharedQueue) { this.sharedQueue = sharedQueue; } public void run() { while (true) { try { sharedQueue.take(); } catch (InterruptedException e) { e.printStackTrace(); } } } } ProductStorage.java

public class ProductStorage { private static BlockingQueue sharedQueue = new ArrayBlockingQueue (20); public static void copyFolder(String newPath, String oldPath) { try { File file = new File(newPath); if (!file.exists()) { file.mkdir(); } SmbFile oldFile = new SmbFile(oldPath); String[] files = oldFile.list(); SmbFile temp = null; for (int i = 0; i < files.length; i++) { if (oldPath.endsWith("/")) { temp = new SmbFile(oldPath + files[i]); } else { temp = new SmbFile(oldPath + File.separator + files[i]); } if (temp.isFile()) { InputStream input = new BufferedInputStream(new SmbFileInputStream(temp)); OutputStream output =new BufferedOutputStream(new FileOutputStream(newPath + File.separator + (temp.getName()).toString())); byte[] b = new byte[1024 * 5000]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); b = new byte[1024 * 5000]; } output.close(); input.close(); } sharedQueue.put(files[i]); if (temp.isDirectory()) { copyFolder(newPath + File.separator + files[i], oldPath + files[i] + "/"); } } } catch (Exception e) { e.printStackTrace(); } } public static void startProductThread(){ System.out.println("--生产者线程执行开始--"); int pThreadSize = 4; ExecutorService pool = Executors.newFixedThreadPool(pThreadSize); for(int i=0;i

Producter.java

public class Producter implements Runnable { private final BlockingQueue sharedQueue; public Producter(BlockingQueue sharedQueue) { this.sharedQueue = sharedQueue; } public void run() { Path path = new Path("E://xxx","smb://xxx:xxx@192.168.233.180/xxx/xxx/"); String newPath = path.getNewPath(); String oldPath = path.getOldPath(); ProductStorage.copyFolder(newPath, oldPath); } } Test.java

public class Test { public static void main(String[] args) { ProductStorage.startProductThread(); ProductStorage.startConsumThread(); } }

本文共计320个文字,预计阅读时间需要2分钟。

生产者消费者模式用队列实现,如何改写为长尾?

javapublic class Consumer implements Runnable { private final BlockingQueue sharedQueue;

public Consumer(BlockingQueue sharedQueue) { this.sharedQueue=sharedQueue; }

生产者消费者模式用队列实现,如何改写为长尾?

public void run() { while (true) { try { sharedQueue.take(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } }}

Consumer.java

public class Consumer implements Runnable { private final BlockingQueue sharedQueue; public Consumer(BlockingQueue sharedQueue) { this.sharedQueue = sharedQueue; } public void run() { while (true) { try { sharedQueue.take(); } catch (InterruptedException e) { e.printStackTrace(); } } } } ProductStorage.java

public class ProductStorage { private static BlockingQueue sharedQueue = new ArrayBlockingQueue (20); public static void copyFolder(String newPath, String oldPath) { try { File file = new File(newPath); if (!file.exists()) { file.mkdir(); } SmbFile oldFile = new SmbFile(oldPath); String[] files = oldFile.list(); SmbFile temp = null; for (int i = 0; i < files.length; i++) { if (oldPath.endsWith("/")) { temp = new SmbFile(oldPath + files[i]); } else { temp = new SmbFile(oldPath + File.separator + files[i]); } if (temp.isFile()) { InputStream input = new BufferedInputStream(new SmbFileInputStream(temp)); OutputStream output =new BufferedOutputStream(new FileOutputStream(newPath + File.separator + (temp.getName()).toString())); byte[] b = new byte[1024 * 5000]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); b = new byte[1024 * 5000]; } output.close(); input.close(); } sharedQueue.put(files[i]); if (temp.isDirectory()) { copyFolder(newPath + File.separator + files[i], oldPath + files[i] + "/"); } } } catch (Exception e) { e.printStackTrace(); } } public static void startProductThread(){ System.out.println("--生产者线程执行开始--"); int pThreadSize = 4; ExecutorService pool = Executors.newFixedThreadPool(pThreadSize); for(int i=0;i

Producter.java

public class Producter implements Runnable { private final BlockingQueue sharedQueue; public Producter(BlockingQueue sharedQueue) { this.sharedQueue = sharedQueue; } public void run() { Path path = new Path("E://xxx","smb://xxx:xxx@192.168.233.180/xxx/xxx/"); String newPath = path.getNewPath(); String oldPath = path.getOldPath(); ProductStorage.copyFolder(newPath, oldPath); } } Test.java

public class Test { public static void main(String[] args) { ProductStorage.startProductThread(); ProductStorage.startConsumThread(); } }