Obviously, the goal of this short tutorial is to teach the concept of thread priority and show how Thread.sleep() works. Navin did a great job explaining those. However, if your goal is to enforce execution order between threads, you can use a binary semaphore like the code below and do not rely on elements of randomness like sleep or thread-scheduling algorithms: ------------------------------------------------------------ import java.util.concurrent.Semaphore; public class Demo { Semaphore goA = new Semaphore(1); Semaphore goB = new Semaphore(0); public static void main(String[] args) { Demo demo = new Demo(); A obj1 = demo.new A(); B obj2 = demo.new B(); obj1.start(); obj2.start(); } class A extends Thread { public void run() { for (int i = 1; i
Will this work ??? class A extends Thread{ // after extending it with thread then now it is a thread // start is a method that belongs to thread class public void run(){ // every thread will have a run method int count1 = 0; for(int i = 0; iHIGHEST PRIORITY // System.out.println("Priority (before) is: " + obj1.getPriority()); // obj1.setPriority(Thread.MAX_PRIORITY); // System.out.println("Priority (after) is: " + obj1.getPriority()); } } // If you have 4 cores then you can execute 4 threads // If you have an octaCore processor then you can execute 8 threads // Number of cores are propotional to number of threads
Obviously, the goal of this short tutorial is to teach the concept of thread priority and show how Thread.sleep() works. Navin did a great job explaining those. However, if your goal is to enforce execution order between threads, you can use a binary semaphore like the code below and do not rely on elements of randomness like sleep or thread-scheduling algorithms:
------------------------------------------------------------
import java.util.concurrent.Semaphore;
public class Demo {
Semaphore goA = new Semaphore(1);
Semaphore goB = new Semaphore(0);
public static void main(String[] args) {
Demo demo = new Demo();
A obj1 = demo.new A();
B obj2 = demo.new B();
obj1.start();
obj2.start();
}
class A extends Thread
{
public void run() {
for (int i = 1; i
Oo😮
by ur saying at 6:50 .Sir, i've kept a time gap of 10 milli secs but still no use, hello was printed twice ,
many times
Depends on how fast your computer is. It would have finished the process pretty fast. Try increasing the sleep time, just play with it .
very nice explanation sir keep it up
We can use serializable for making it sequence?
Thank you sir❤❤
awesome!
its hard to split de HI. and HELLO.. i couldnt but it cool!!
public class MultipleThreads {
public static void main(String[] args) {
printHello o = new printHello();
printHi i = new printHi();
o.start();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
i.start();
}
}
class printHello extends Thread {
public void printHello() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " : " + "hello");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
class printHi extends Thread {
public void printHi() {
for (int i = 1; i < 10; i++) {
System.out.println(i + " : " + "hi");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
Wait & Try Catch
Will this work ???
class A extends Thread{ // after extending it with thread then now it is a thread
// start is a method that belongs to thread class
public void run(){
// every thread will have a run method
int count1 = 0;
for(int i = 0; iHIGHEST PRIORITY
// System.out.println("Priority (before) is: " + obj1.getPriority());
// obj1.setPriority(Thread.MAX_PRIORITY);
// System.out.println("Priority (after) is: " + obj1.getPriority());
}
}
// If you have 4 cores then you can execute 4 threads
// If you have an octaCore processor then you can execute 8 threads
// Number of cores are propotional to number of threads
I have 6-core processor and tried 7 threads but it worked, any clue why?
We appreciate👍
Brilliant.
Missing Hello Aliens!!
❤
my machine is printing HIHI ns hello hello even if we apply sleep for both the Threads.