Home Answers Viewqa Java-Beginners Multi Threading is not working in that why...?

 
 


Rahul Shivhare
Multi Threading is not working in that why...?
0 Answer(s)      10 months ago
Posted in : Java Beginners

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class FileSpliting {

static byte[] dataBytes1 = new byte[100000000]; static byte[] dataBytes2 = new byte[100000000];

public static void main(String[] args) throws IOException { String filename = "users14.dbf"; System.out.println("File Name---"+filename); File file = new File(filename); System.out.println("size---"+file.length());

long sz=file.length();
int totalChunk=(int) (sz/100000000)+1;
int len = 0;
try {
    InputStream fp = new FileInputStream(file);
    int k=1;
    int i=0;
    int nextBuffer=1;

    while (totalChunk>0) {
        if (nextBuffer == 1)
        {
            len = fp.read(dataBytes1);  //read(dataBytes,(int) ps,1000);
            nextBuffer = 2;
        }
        else
        {
            len = fp.read(dataBytes2);  //read(dataBytes,(int) ps,1000);
            nextBuffer = 1;
        }
        i = i + len;
        System.out.println("Remaining-------------------------"+(sz-i));
        while (Thread.activeCount() == 5)
        {
            Thread.sleep(1);
        }

        if (nextBuffer == 2)
            new NewThread(filename,k,1,len);
        else
            new NewThread(filename,k,2,len);

        k++;
        totalChunk--;
    }
    file=null;
    while (Thread.activeCount() == 2)
    {
        Thread.sleep(1);
    }
    Date date= new Date();
    System.out.println("end of file---"+date);
}catch (Exception e) {
    e.printStackTrace();
}

} } class NewThread implements Runnable { Thread t; private String filename=null; private int j; File file=null; long len; private int no; //byte[] dataBytes1 = new byte[10000000]; FileSpliting fs=new FileSpliting();

NewThread(String filname,int j, int no,long len) { this.filename=filname; this.j=j; //this.dataBytes1=dataBytes1; this.len=len; this.no=no; t = new Thread(this, "file Thread"); // System.out.println("Child thread: " + t); t.start(); // Start the thread } @SuppressWarnings("static-access") public void run() {

File file1 = new File("ddd/"+j+"_"+filename);
try {
    System.out.println("success---"+file1.createNewFile());
    FileOutputStream myStream = new FileOutputStream(file1);
    if(no==1)
        myStream.write(fs.dataBytes1,0,(int)len);
    else
        myStream.write(fs.dataBytes2,0,(int)len);
    myStream.close();

} catch (Exception e) {
    e.printStackTrace();
}

} }

View Answers









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.