0

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
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Here is the code that I have working. I am not sure why you passed the counter to ConThread but I have set it to 0. Since the counter is static, I am using synchronized static method to change the counter. You can read about it here.

Here's the code:

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();
        }
    }

The output:

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
kks21199
  • 1,116
  • 2
  • 10
  • 29
  • 1
    Thank you sooooooo so so much. You made my day. It worked perfect. You´re a genius :) I have searched so long for a solution.. – Leo_East93 Nov 09 '20 at 20:29