Java MultiThread Örnek Soru Çözümü

ilyasaglar tarafından tarihinde yayınlandı

ÖRNEK SORU : 
1’den 100.000’e kadarki sayılar arasında maksimum bölen sayisi olan sayiyi ve bolen sayisini bulmak istiyoruz.
Bunu 4 paralel proseste yapalım. Bunun için 4 thread oluşturulacak.Her bir thread’e mainde name set edilecek.
(Örnek: Thread t1=new Thread();
t1.setName(“1”); şeklinde.)

Her bir thread ilgili aralıktaki maksimum bölen sayisi olan sayiyi ve bolen sayisini kendi isminde bir txt dosyasına yazdıracak.(Örnek:
1.thread 1-25000
2.thread 25000-50000
3.thread 50000-75000
4.thread 75000-100000 aralığında)

Main her 2 sn’de bir dosyaları kontrol edecek.( Örnek:Thread.sleep(2000); ) 
Tüm dosyalar oluşmuşsa o dosyalardan verileri okuyup 1-100000 arasında maksimum bölen sayısı olan sayıyı ve bölen sayısını yazdıralım.

 

 

CEVAP: 

 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

class Bolen extends Thread {

int lowLimit, highLimit;

public Bolen(int lowLimit, int highLimit) {
super();
this.lowLimit = lowLimit;
this.highLimit = highLimit;
}

@Override
public void run() {
int bolenSayisi = 0;
int maxBolenSayisi = 0;
int maxIndex = 0;
long startTime = System.nanoTime();
String fileName = this.getName() + “.txt”;

for (int i = lowLimit; i <= highLimit; i++) {
bolenSayisi = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
bolenSayisi++;
}
}
if (bolenSayisi > maxBolenSayisi) {
maxBolenSayisi = bolenSayisi;
maxIndex = i;

}

}
System.out.println(this.getName() + ” : ” + maxIndex + ” ” + maxBolenSayisi);

long endTime = System.nanoTime();
double duration = (endTime – startTime) / 1000000;
System.out.println(“Geçen süre: ” + duration);

try {
FileWriter fw = new FileWriter(fileName);
fw.write(maxIndex + ” ” + maxBolenSayisi);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

class FileLine {
int sayi;
int bolenSayisi;

public FileLine(int sayi, int bolenSayisi) {
this.sayi = sayi;
this.bolenSayisi = bolenSayisi;
}

}

public class TestThread {

public static void main(String[] args) throws FileNotFoundException, InterruptedException {

Bolen thread1 = new Bolen(1, 50000);
thread1.setName(“1”);
thread1.start();

Bolen thread2 = new Bolen(50001, 70000);
thread2.setName(“2”);
thread2.start();

Bolen thread3 = new Bolen(70001, 87000);
thread3.setName(“3”);
thread3.start();

Bolen thread4 = new Bolen(87001, 100000);
thread4.setName(“4”);
thread4.start();

File f1 = new File(thread1.getName() + “.txt”);
File f2 = new File(thread2.getName() + “.txt”);
File f3 = new File(thread3.getName() + “.txt”);
File f4 = new File(thread4.getName() + “.txt”);

FileLine[] flArray = new FileLine[4];

int maxBolenSayisi = 0;
int sayi = 0;

while (true) {
Thread.sleep(10000);

if (f1.exists() && f2.exists() && f3.exists() && f4.exists()) {

flArray[0] = getLine(f1);
flArray[1] = getLine(f2);
flArray[2] = getLine(f3);
flArray[3] = getLine(f4);

for (FileLine fileLine : flArray) {
if (fileLine.bolenSayisi > maxBolenSayisi) {
maxBolenSayisi = fileLine.bolenSayisi;
sayi = fileLine.sayi;
}
}
break;
}

}

System.out.println(“Sayi:” + sayi + ” bolen sayisi:” + maxBolenSayisi);

}

private static FileLine getLine(File f) throws FileNotFoundException {
Scanner sc = new Scanner(f);

FileLine fl = new FileLine(sc.nextInt(), sc.nextInt());
sc.close();
return fl;
}

}

 

EKRAN ÇIKTISI: 

 


0 yorum

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir