Guest User

Untitled

a guest
Mar 14th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1.     import java.io.File;
  2.     import java.io.FileNotFoundException;
  3.     import java.io.FileOutputStream;
  4.     import java.io.IOException;
  5.  
  6.     public class DiskFolderCreationThread implements Runnable{
  7.  
  8.     @Override
  9.     public void run() {
  10.         // TODO Auto-generated method stub
  11.         long randData = (long) (System.currentTimeMillis() + (Math.random() * 100000));
  12.         String folderPath = File.separatorChar + "data" + File.separatorChar + "ext" + File.separatorChar + randData;
  13.         File testFolder = new File(folderPath);
  14.         if (!testFolder.exists()) {
  15.             if(testFolder.mkdirs() == false)
  16.                 System.out.println(System.currentTimeMillis() + ": folder creation failed!");
  17.  
  18.         }
  19.         String filename = testFolder + File.separator + "test.txt";
  20.         File testFile = new File(filename);
  21.         FileOutputStream fileOutputStream = null;
  22.         boolean bCreated = false;
  23.         try {
  24.             //Thread.sleep(1000);
  25.             if(testFolder.exists() == false) {
  26.                 System.out.println("Folder not created!");
  27.                 System.out.println("Folder: " + testFolder.getAbsolutePath() + " does not exist!");
  28.             }else
  29.                 bCreated = true;
  30.             fileOutputStream = new FileOutputStream(testFile);
  31.             fileOutputStream.close();
  32.             testFile.delete();
  33.             testFolder.delete();
  34.  
  35.             //System.out.println("success:" + testFile.getAbsolutePath());
  36.         } catch (Exception e) {
  37.             // TODO Auto-generated catch block
  38.             e.printStackTrace();
  39.             System.out.println("Folder created? " + testFolder + ": " + bCreated);
  40.             //back off and see if the folder is really created
  41.             try {
  42.                 Thread.sleep(1000);
  43.             } catch (InterruptedException e1) {
  44.                 // TODO Auto-generated catch block
  45.                 e1.printStackTrace();
  46.             }
  47.             System.out.println("Checking again, " + testFolder.getAbsolutePath() + " created? " + testFolder.exists());
  48.         }
  49.    
  50.     }
  51.     }
  52.  
  53. Loader class
  54.  
  55.     import java.util.concurrent.Executor;
  56.     import java.util.concurrent.Executors;
  57.    
  58.     public class DiskFolderTest {
  59.         public void start() {
  60.             Executor executorService = Executors.newFixedThreadPool(200);
  61.             for(int i=0;i<10000;i++)
  62.                 executorService.execute(new DiskFolderCreationThread());
  63.         }
  64.    
  65.         public static void main(String args[]) {
  66.             new DiskFolderTest().start();
  67.         }
  68.     }
  69.  
  70.  Output:
  71.  
  72.     java.io.FileNotFoundException: \data\ext\1521045935714\test.txt (The system cannot find the path specified)
  73.         at java.io.FileOutputStream.open0(Native Method)
  74.         at java.io.FileOutputStream.open(FileOutputStream.java:270)
  75.         at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
  76.         at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
  77.         at DiskFolderCreationThread.run(DiskFolderCreationThread.java:30)
  78.         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  79.         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  80.         at java.lang.Thread.run(Thread.java:745)
  81.     Folder created? \data\ext\1521045935714: true
  82.     Checking again, C:\data\ext\1521045935714 created? false
Add Comment
Please, Sign In to add comment