我有一个关于我做的练习的问题。
我想创建1,2,4,5,10和20个线程。第一个线程应该单独打开20个网站。网页的内容并不重要。在他做了之后,2个线程应该打开相同的20个网站。之后4线程相同的20个网站等等。
学习处理线程是一个很好的练习。我想停止时间,想展示,有太多线索并不理想,恰恰相反。
我已经试了两周了,但无法解决。我有一些好主意,但不是100%正确。这里有一些代码示例:
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();
}
}
}
第一个问题是我的线程同步不起作用。第二个问题是它应该删除所有线程(threadsList.clear((;(在每个循环之后,再次从Thread-0开始,但不是。第三个问题是sysout在几个循环之后给了我null。所以(就像问题一(同步是错误的。。。
这是我得到的结果
(图片不起作用,所以这里只有几行…(…
* * * * * * * * * * * * 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
这是我正在处理的代码。我不知道为什么您将counter
传递给ConThread
,但我已将其设置为0。由于计数器是静态的,我使用同步静态方法来更改计数器。你可以在这里阅读。
这是代码:
ConThread.java
public class ConThread extends Thread {
int name;
static Integer counter = 0;
ConcurrentLinkedQueue<String> urlList;
public ConThread(int name, ConcurrentLinkedQueue<String> urlList) {
this.name = name;
this.urlList = urlList;
}
@Override
public void run() {
while(counter < 20) { //while counter is less than 20, keep polling for new url
try {
String url = urlList.poll();
raiseCount();
//Add your Http request code here
sleep(200);
System.out.println(getName() + " Thread - " + name + " -- " + url + " --- " + counter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}
//Change all methods related to count to synchronized
public static synchronized void raiseCount() {
counter=counter+1;
}
public static synchronized Integer getCount() {
return counter;
}
public static synchronized void setCount(Integer num) {
counter= num;
}
}
Main.java
public static void main(String args[]) {
ArrayList<ConThread> threadsList = new ArrayList<>();
ConcurrentLinkedQueue<String> data = new ConcurrentLinkedQueue<>();
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) {
//Scan the file to add websites. I am manually adding
for(int i = 1; i <=20; i++)
data.add("Url " + String.valueOf(i));
for (int i = 0; i < j; i++) {
ConThread t = new ConThread(i, data);
threadsList.add(t);
}
long startTime = System.currentTimeMillis(); //only start the timer before starting the thread
ConThread.setCount(0); //Set counter to 0
for (ConThread p : threadsList) {
p.start();
}
for (ConThread threadW : threadsList) {
try {
threadW.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
long diffTime = endTime - startTime;
threadsList.clear();
threadsList = new ArrayList<>(); //Setting it as new will clear all values.
System.out.println("* * * * * * * * * * * * End...");
System.out.println("* * * * * * * * * * * * Time needed with " + j + " Threads: " + diffTime);
System.out.println();
System.out.println();
}
}
输出:
Thread-0 Thread - 0 -- Url 1 --- 1
Thread-0 Thread - 0 -- Url 2 --- 2
Thread-0 Thread - 0 -- Url 3 --- 3
Thread-0 Thread - 0 -- Url 4 --- 4
Thread-0 Thread - 0 -- Url 5 --- 5
Thread-0 Thread - 0 -- Url 6 --- 6
Thread-0 Thread - 0 -- Url 7 --- 7
Thread-0 Thread - 0 -- Url 8 --- 8
Thread-0 Thread - 0 -- Url 9 --- 9
Thread-0 Thread - 0 -- Url 10 --- 10
Thread-0 Thread - 0 -- Url 11 --- 11
Thread-0 Thread - 0 -- Url 12 --- 12
Thread-0 Thread - 0 -- Url 13 --- 13
Thread-0 Thread - 0 -- Url 14 --- 14
Thread-0 Thread - 0 -- Url 15 --- 15
Thread-0 Thread - 0 -- Url 16 --- 16
Thread-0 Thread - 0 -- Url 17 --- 17
Thread-0 Thread - 0 -- Url 18 --- 18
Thread-0 Thread - 0 -- Url 19 --- 19
Thread-0 Thread - 0 -- Url 20 --- 20
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 1 Threads: 4014
Thread-2 Thread - 1 -- Url 2 --- 2
Thread-1 Thread - 0 -- Url 1 --- 2
Thread-1 Thread - 0 -- Url 4 --- 4
Thread-2 Thread - 1 -- Url 3 --- 4
Thread-2 Thread - 1 -- Url 6 --- 6
Thread-1 Thread - 0 -- Url 5 --- 6
Thread-1 Thread - 0 -- Url 8 --- 8
Thread-2 Thread - 1 -- Url 7 --- 8
Thread-2 Thread - 1 -- Url 10 --- 10
Thread-1 Thread - 0 -- Url 9 --- 10
Thread-1 Thread - 0 -- Url 12 --- 12
Thread-2 Thread - 1 -- Url 11 --- 12
Thread-1 Thread - 0 -- Url 13 --- 14
Thread-2 Thread - 1 -- Url 14 --- 14
Thread-1 Thread - 0 -- Url 15 --- 16
Thread-2 Thread - 1 -- Url 16 --- 16
Thread-1 Thread - 0 -- Url 17 --- 18
Thread-2 Thread - 1 -- Url 18 --- 18
Thread-1 Thread - 0 -- Url 19 --- 20
Thread-2 Thread - 1 -- Url 20 --- 20
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 2 Threads: 2007
Thread-6 Thread - 3 -- Url 4 --- 4
Thread-5 Thread - 2 -- Url 3 --- 4
Thread-3 Thread - 0 -- Url 1 --- 4
Thread-4 Thread - 1 -- Url 2 --- 4
Thread-4 Thread - 1 -- Url 8 --- 8
Thread-6 Thread - 3 -- Url 5 --- 8
Thread-5 Thread - 2 -- Url 6 --- 8
Thread-3 Thread - 0 -- Url 7 --- 8
Thread-4 Thread - 1 -- Url 9 --- 12
Thread-6 Thread - 3 -- Url 10 --- 12
Thread-3 Thread - 0 -- Url 12 --- 12
Thread-5 Thread - 2 -- Url 11 --- 12
Thread-5 Thread - 2 -- Url 16 --- 16
Thread-6 Thread - 3 -- Url 14 --- 16
Thread-3 Thread - 0 -- Url 15 --- 16
Thread-4 Thread - 1 -- Url 13 --- 16
Thread-6 Thread - 3 -- Url 18 --- 20
Thread-5 Thread - 2 -- Url 17 --- 20
Thread-3 Thread - 0 -- Url 19 --- 20
Thread-4 Thread - 1 -- Url 20 --- 20
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 4 Threads: 1003
Thread-10 Thread - 3 -- Url 4 --- 8
Thread-11 Thread - 4 -- Url 5 --- 8
Thread-7 Thread - 0 -- Url 1 --- 8
Thread-12 Thread - 5 -- Url 8 --- 8
Thread-9 Thread - 2 -- Url 3 --- 8
Thread-13 Thread - 6 -- Url 7 --- 8
Thread-8 Thread - 1 -- Url 2 --- 8
Thread-14 Thread - 7 -- Url 6 --- 8
Thread-12 Thread - 5 -- Url 12 --- 16
Thread-14 Thread - 7 -- Url 16 --- 16
Thread-9 Thread - 2 -- Url 13 --- 16
Thread-11 Thread - 4 -- Url 10 --- 16
Thread-13 Thread - 6 -- Url 14 --- 16
Thread-8 Thread - 1 -- Url 15 --- 16
Thread-10 Thread - 3 -- Url 9 --- 16
Thread-7 Thread - 0 -- Url 11 --- 16
Thread-14 Thread - 7 -- Url 18 --- 20
Thread-11 Thread - 4 -- Url 20 --- 20
Thread-12 Thread - 5 -- Url 17 --- 20
Thread-9 Thread - 2 -- Url 19 --- 20
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 8 Threads: 605
Thread-15 Thread - 0 -- Url 1 --- 10
Thread-16 Thread - 1 -- Url 2 --- 10
Thread-23 Thread - 8 -- Url 7 --- 12
Thread-17 Thread - 2 -- Url 5 --- 12
Thread-22 Thread - 7 -- Url 6 --- 12
Thread-18 Thread - 3 -- Url 3 --- 12
Thread-19 Thread - 4 -- Url 4 --- 12
Thread-21 Thread - 6 -- Url 9 --- 17
Thread-24 Thread - 9 -- Url 10 --- 17
Thread-20 Thread - 5 -- Url 8 --- 17
Thread-16 Thread - 1 -- Url 12 --- 20
Thread-15 Thread - 0 -- Url 11 --- 20
Thread-17 Thread - 2 -- Url 14 --- 20
Thread-18 Thread - 3 -- Url 16 --- 20
Thread-22 Thread - 7 -- Url 15 --- 20
Thread-23 Thread - 8 -- Url 13 --- 20
Thread-24 Thread - 9 -- Url 19 --- 20
Thread-20 Thread - 5 -- Url 20 --- 20
Thread-19 Thread - 4 -- Url 17 --- 20
Thread-21 Thread - 6 -- Url 18 --- 20
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 10 Threads: 404
Thread-25 Thread - 0 -- Url 1 --- 20
Thread-31 Thread - 6 -- Url 7 --- 20
Thread-32 Thread - 7 -- Url 8 --- 20
Thread-27 Thread - 2 -- Url 3 --- 20
Thread-28 Thread - 3 -- Url 6 --- 20
Thread-29 Thread - 4 -- Url 5 --- 20
Thread-39 Thread - 14 -- Url 15 --- 20
Thread-30 Thread - 5 -- Url 4 --- 20
Thread-26 Thread - 1 -- Url 2 --- 20
Thread-37 Thread - 12 -- Url 13 --- 20
Thread-36 Thread - 11 -- Url 12 --- 20
Thread-40 Thread - 15 -- Url 14 --- 20
Thread-35 Thread - 10 -- Url 11 --- 20
Thread-34 Thread - 9 -- Url 10 --- 20
Thread-33 Thread - 8 -- Url 9 --- 20
Thread-38 Thread - 13 -- Url 16 --- 20
Thread-43 Thread - 18 -- Url 19 --- 20
Thread-42 Thread - 17 -- Url 18 --- 20
Thread-44 Thread - 19 -- Url 20 --- 20
Thread-41 Thread - 16 -- Url 17 --- 20
* * * * * * * * * * * * End...
* * * * * * * * * * * * Time needed with 20 Threads: 204
Process finished with exit code 0