Witam otóż musze napisać program który korzysta z wątków tak aby przyspieszyc dzialanie programu, wlasciwego programu nie bede zamieszczal ale stworzylem cos bazujacego na tym programie i uzylem watkow. Problem jest w tym ze nie wiem czy zrobilem to dobrze(wedlug mnie nie ma zadnej roznicy), a jak nie to jak to sie robi bo moze zle rozumuje w tym temacie. Z gory dziekuje za pomoc.
// NewClass
package test2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewClass implements Runnable
{
private final int i;
NewClass(int i) {
this.i = i;
}
@Override
public void run()
{
LinkedList <String> lista1 = new LinkedList<String>();
File file = new File("pliczek.txt");
int dlugosc = 500000;
//tworzenie
for(int i = 0 ; i<dlugosc ; i++)
{
int x = i+2;
i++;
String a = Integer.toString(x);
lista1.add(a);
}
//zapisywanie
try {
try (PrintWriter pr = new PrintWriter(file)) {
for(String n : lista1)
{
pr.println(n);
}
}
lista1.clear();
} catch (FileNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// NewClass2
package test2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewClass2 implements Runnable
{
private final int i;
NewClass2(int i) {
this.i = i;
}
public void run()
{
try {
Thread.sleep(150);
File file = new File("pliczek.txt");
// odczytywanie
LinkedList <String> lista2 = new LinkedList<String>();
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = "";
while(line != null)
{
try {
line = br.readLine();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
lista2.add(line);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
//wypisanie odczytanych
lista2.removeLast();
for(String m : lista2)
{
System.out.println(m);
}
lista2.clear();
} catch (InterruptedException ex) {
Logger.getLogger(NewClass2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// Test2
package test2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test2
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
Runnable[] runners = new Runnable[1];
Thread[] threads = new Thread[1];
for(int i=0; i<1; i++) {
runners[i] = new NewClass(i);
}
for(int i=0; i<1; i++) {
threads[i] = new Thread(runners[i]);
}
for(int i=0; i<1; i++) {
threads[i].start();
}
Runnable[] runners2 = new Runnable[1];
Thread[] threads2 = new Thread[1];
for(int i=0; i<1; i++) {
runners2[i] = new NewClass2(i);
}
for(int i=0; i<1; i++) {
threads2[i] = new Thread(runners2[i]);
}
for(int i=0; i<1; i++) {
threads2[i].start();
}
}
}