Skip to content

Instantly share code, notes, and snippets.

@etemesi254
Created June 29, 2022 17:22
Show Gist options
  • Save etemesi254/0112099a7bc76ca803a3af29c3f477a7 to your computer and use it in GitHub Desktop.
Save etemesi254/0112099a7bc76ca803a3af29c3f477a7 to your computer and use it in GitHub Desktop.
package org.example;
// Java code for thread creation by extending
// the Thread class
class MultithreadingDemo extends Thread {
int num = 433452;
public void run()
{
try {
num /= Thread.currentThread().getId();
System.out.println(num);
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
public class MultiThreaded {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment