I have a question regarding an exercise I got.
I want to create 1,2,4,5,10 and 20 threads. First 1 Thread should open 20 website alone. The content of the web pages is not important. After he did it, then 2 threads should open the same 20 website. After that 4 threads the same 20 websites and so on.
It´s a nice exercise to learn to handle with threads. I want to stop time and want to show, that's not ideal to have too much threads and the opposite of that.
I am trying it for 2 weeks know, but could not solve it. I got some good ideas but it was not 100% correct. Here some code examples:
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConThread extends Thread{
int name;
static Integer counter = 0;
ConcurrentLinkedQueue<String> liste;
int count2;
boolean stat = false;
String data ;
public ConThread(int name, ConcurrentLinkedQueue<String> liste, Integer counter) {
this.name = name;
this.liste = liste;
//this.counter = counter;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 1 ; i < 21 ; i++) {
synchronized(counter) {
if(liste.peek()==null) {
} else {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(liste.peek()))
.build();
try {
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(liste.size() + " " + getName() + " " + liste.poll());
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
raiseCount();
if(getCount() == 20) {
setCount(0);
}
}
}
}
public void raiseCount() {
counter=counter+1;
}
public Integer getCount() {
return counter;
}
public void setCount(Integer num) {
counter= num;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Main {
public static void main(String args[]) {
Integer counter = 0;
ArrayList<ConThread> threadsList = new ArrayList<>();
ConcurrentLinkedQueue<String> data = new ConcurrentLinkedQueue<String>();
ArrayList<Integer> threads = new ArrayList<Integer>();
threads.add(1);threads.add(2);threads.add(4);threads.add(8);threads.add(10);threads.add(20);
for(Integer j : threads) {
//******* File reading
Scanner input = null;
try {
//* * * * * * * * * * * * * * * * *
//---> ADD PATH TO TEXT FILE !!!
//* * * * * * * * * * * * * * * * *
input = new Scanner(new File("/Users/c/Desktop/Aufgabe1_Nachrichtenseiten.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("Text-file in wrong location!");
System.out.println("Change directory from text-file or path from scanner.");
}
while(input.hasNextLine())
{
data.add(input.nextLine());
}
input.close();
threadsList.clear();
System.out.println("* * * * * * * * * * * * Start...");
//System.out.println();
long startTime = System.currentTimeMillis();
for (int i = 0; i < j; i++) {
ConThread t = new ConThread(i, data, counter);
threadsList.add(t);
//t.start();
}
for(ConThread p : threadsList) {
p.start();
}
for (ConThread threadW : threadsList) {
try {
threadW.join();
//threadW.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
threadsList.clear();
long endTime = System.currentTimeMillis();
long diffTime= endTime - startTime;
//System.out.println();
System.out.println("* * * * * * * * * * * * End...");
System.out.println("* * * * * * * * * * * * Time needed with " + j + " Threads: " + diffTime);
System.out.println();
System.out.println();
}
}
}
First problem is that something doesn't work with the synchronization of my threads. The second problem is it should delete all threads (threadsList.clear();) after each loop and start with Thread-0 again, but it isn't. The third problem is that the sysout gives me null after a couple of loops. So the (like problem one) synchronization is wrong...
here the result I got
(picture does not work, so here are only a couple of lines...) ...
* * * * * * * * * * * * Start...
20 Thread-7 https://www.spiegel.de/
19 Thread-7 https://www.zeit.de/
18 Thread-14 https://www.nst.com.my
17 Thread-14 https://www.smh.com.au
16 Thread-7 https://www.nzz.ch/
15 Thread-13 https://www.krone.at/
14 Thread-14 https://www.yomiuri.co.jp
13 Thread-7 https://timesofindia.indiatimes.com
12 Thread-12 https://www.nytimes.com
11 Thread-13 https://norwaytoday.info
10 Thread-14 http://www.cankaoxiaoxi.com/
9 Thread-14 https://www.dailymail.com
8 Thread-12 https://www.batimes.com.ar
7 Thread-7 https://news.chosun.com
6 Thread-11 https://www.thairath.co.th/
5 Thread-12 http://english.ahram.org.eg/
4 Thread-7 https://jang.com.pk/
3 Thread-7 https://www.ouest-france.fr/
2 Thread-12 https://www.kp.ru/
1 Thread-12 https://www.telegraaf.nl
0 Thread-11 null
0 Thread-7 null
0 Thread-10 null
0 Thread-14 null
0 Thread-13 null
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 8 Threads: 5930
* * * * * * * * * * * * Start...
20 Thread-15 https://www.spiegel.de/
19 Thread-24 https://www.zeit.de/
18 Thread-15 https://www.nst.com.my
17 Thread-23 https://www.smh.com.au
16 Thread-24 https://www.nzz.ch/
15 Thread-15 https://www.krone.at/
14 Thread-22 https://www.yomiuri.co.jp
13 Thread-23 https://timesofindia.indiatimes.com
12 Thread-24 https://www.nytimes.com
11 Thread-15 https://norwaytoday.info
10 Thread-23 http://www.cankaoxiaoxi.com/
9 Thread-23 https://www.dailymail.com
8 Thread-21 https://www.batimes.com.ar
7 Thread-22 https://news.chosun.com
6 Thread-20 https://www.thairath.co.th/
5 Thread-22 http://english.ahram.org.eg/
4 Thread-21 https://jang.com.pk/
3 Thread-15 https://www.ouest-france.fr/
2 Thread-22 https://www.kp.ru/
1 Thread-21 https://www.telegraaf.nl
0 Thread-22 null
0 Thread-19 null
0 Thread-20 null
0 Thread-24 null
0 Thread-15 null
0 Thread-23 null
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 10 Threads: 4078