0

My system Information says this
Processor Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz, 1992 Mhz, 4 Core(s), 8 Logical Processor(s)

So, I wrote 9 classes and called all. Since there are only 4 core and 8 Logical Processor(s), How all the nine functions printed parallelly?

Does it mean one core is handling two threads?

class Hi extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hi");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}

class Hello extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}

class Hello1 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello1");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}

class Hello2 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello2");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}


class Hello3 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello3");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}

class Hello4 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello4");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}
class Hello5 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello5");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}

class Hello6 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello6");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}

class Hello7 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Hello7");
            try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
        }
    }
}

public class JavaThread_1 {
    public static void main(String[] args) {
        Hi hi = new Hi();
        Hello hello = new Hello();
        Hello1 hello1 = new Hello1();
        Hello2 hello2= new Hello2();
        Hello3 hello3 = new Hello3();
        Hello4 hello4 = new Hello4();
        Hello5 hello5 = new Hello5();
        Hello6 hello6 = new Hello6();
        Hello7 hello7 = new Hello7();

        hi.start();
        hello.start();
        hello1.start();
        hello2.start();
        hello3.start();
        hello4.start();
        hello5.start();
        hello6.start();
        hello7.start();
    }
}

output

Hi
Hello3
Hello2
Hello1
Hello
Hello5
Hello4
Hello6
Hello7
..
...
.... same thing continues for every 500 ms but how 9 threads executed simultaneously I'm not getting
Vikas Acharya
  • 3,550
  • 4
  • 19
  • 52
  • 1
    The CPUs switch between threads, so you can run many threads with even a single CPU. It just works on one for a few milliseconds, then switches to running another. – tgdavies Dec 18 '20 at 05:53
  • @tgdavies Thank you, I'm trying to execute the limitation point of threads. Can you kindly help me. – Vikas Acharya Dec 18 '20 at 05:56
  • Have a look at this: https://stackoverflow.com/questions/21650083/maximum-number-of-threads-than-can-run-concurrently-in-java-on-a-cpu – tgdavies Dec 18 '20 at 05:58
  • Even a single-CPU computer can run all the threads in parallel, because when a thread goes to sleep, the CPU can run another thread. – Andreas Dec 18 '20 at 06:26
  • @EricSchaefer Thank you, I visited there but didn't get any practical example on how to show that. I'm looking for a code that throws thread overflow error. Can you kindly help me on that. – Vikas Acharya Dec 18 '20 at 08:03
  • There is no such thing as a "thread overflow error". You are completely misunderstanding how Multitasking/Multitheading works. The only limit to the number of threads you can launch is your memory. – EricSchaefer Dec 18 '20 at 08:34
  • @EricSchaefer I need to know more about that. What if memory is not there? kindly provide some very good docs on that. – Vikas Acharya Dec 18 '20 at 08:36
  • That's not something that can be explained in a post or comment. In general if memory runs out an OutOfMemoryExecption is thrown. You should read up about multitasking, multithreading and (Java) memory management. If you google these terms a ton of information should come up. – EricSchaefer Dec 18 '20 at 12:13

0 Answers0