Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Created February 6, 2016 01:01
Show Gist options
  • Select an option

  • Save Viacheslav77/eacc9006b31b7c6b62bb to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/eacc9006b31b7c6b62bb to your computer and use it in GitHub Desktop.
Написать класс, который позволит записывать текстовые данные в один файл из разных потоков. Более сложное решение: код читает и записывает несколькими потоками при этом собирает правильно фразу
package FileWriteTread;
public class Data {
static byte [] data;
int j;
public byte [] getData () {
return data;
}
public void setData (byte [] data) {
this.data=data;
}
public int getJ () {
return j;
}
public void setJ (int j) {
this.j=j;
}
}
package FileWriteTread;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.RandomAccess;
public class FileWrite extends Thread {
byte [] buf;
static int cursor;
static int nuberStream;
Data data;
File file;
public FileWrite (Data data, File file) throws FileNotFoundException, IOException{
this.data=data;
this.file=file;
}
public void run(){
System.out.println("Ожидаем WordToByte: " + getId());
synchronized (data) {
try {
//while ( nuberStream!=data.getJ())
data.wait();
} catch (InterruptedException e) {
return;
}
}
buf = data.getData();
//System.out.println(to.word.getBytes()+ "-----------------");
try {
RandomAccessFile out = new RandomAccessFile(file,"rw");
try {
out.seek (cursor);
out.write(buf,0,buf.length);
cursor+=buf.length;
} finally {
out.close();
}
} catch (IOException e) {
System.out.println("IOExeption");
}
}
}
package FileWriteTread;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
/* Написать класс, который позволит записывать текстовые данные в один файл из разных потоков.
Более сложное решение: код читает и записывает несколькими потоками при этом собирает правильно фразу */
public class MyClass {
public static void main(String[]args) throws FileNotFoundException, IOException{
File file=new File("d:\\1\\FileWriteThreat.txt");
try {
file.delete();
}catch(Exception e){
System.out.println(e);
}
Data da = new Data();
String[] text = {"Hello", "World","!"};
ArrayList<Thread> threadList=new ArrayList<>();
for(int i=0;i<text.length ;i++){
threadList.add(new FileWrite(da, file));
}
for(Thread thread : threadList){
thread.start();
}
ArrayList<Thread> threadList1=new ArrayList<>();
for(int i=0;i<text.length;i++){
threadList1.add(new threadOne(text[i]+" ",da, file ));
}
for(Thread thread : threadList1){
thread.start();
try{
thread.join();
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}
package FileWriteTread;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class threadOne extends Thread {
String word;
int numberThread;
String pathFile;
static int j = 1;
byte[] buf;
File file;
Data data;
public threadOne (String word, Data data, File file){
this.word=word;
this.file=file;
this.data=data;
}
public void run (){
buf = word.getBytes();
System.out.println("Запустился поток : " +getId() + " " + j + " " + word);
j++;
synchronized (data){
data.setData(buf);
data.setJ(numberThread);
data.notify();
}
}
public byte[] getBuf(){
return buf;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment