Skip to content

Instantly share code, notes, and snippets.

@xProgrammer-007
Created August 18, 2021 09:09
Show Gist options
  • Select an option

  • Save xProgrammer-007/431faff559e99c7a536b31e295d23158 to your computer and use it in GitHub Desktop.

Select an option

Save xProgrammer-007/431faff559e99c7a536b31e295d23158 to your computer and use it in GitHub Desktop.
public class ThreadingProg {
public static void main (String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("1st Thread spawned");
int random = (int)(Math.random() * 1000 + 1);
System.out.println("Random number is " + random);
if(random %2 == 0){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("2nd Thread spawned");
System.out.println("Square of " + random + " is = " + random * random);
}
}).start();
}else{
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("2nd Thread spawned");
System.out.println("Cube of " + random + " is = " + random * random * random);
}
}).start();
}
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment