Advertisement
Oppenheimer

MultiThreading 1

Feb 23rd, 2025
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class A{
  4.     // im here to just chill
  5.     int counter=0;
  6.  
  7.     // synchronized : to make a method accessible to single thread at a time
  8.     public synchronized void increaseCounter(){
  9.         counter++;
  10.     }
  11. }
  12.  
  13. class ThreadViaClass extends Thread{
  14.    
  15.     String output;
  16.  
  17.     ThreadViaClass(String output){
  18.         this.output = output;
  19.     }
  20.     // necessary function
  21.     public void run(){
  22.         for(int i=0;i<10;i++){
  23.             System.out.println(this.output);
  24.            
  25.             // as sleep can throw exception
  26.             try{
  27.                 Thread.sleep(500);
  28.             }
  29.             catch(Exception e){
  30.                 System.out.println(e);
  31.             }
  32.  
  33.  
  34.         }
  35.     }
  36. }
  37.  
  38. // as there is no multiple inheritance in java :(
  39. class ThreadViaInterface extends A implements Runnable{
  40.     String output;
  41.  
  42.     ThreadViaInterface(String output){
  43.         this.output = output;
  44.     }
  45.    
  46.     public void run(){
  47.         for(int i=0;i<10;i++){
  48.             System.out.println(this.output);
  49.  
  50.             try{
  51.                 Thread.sleep(500);
  52.             }
  53.             catch(Exception e){
  54.                 System.out.println(e);
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. public class Main{
  61.     // as Thread.join() throws an exception
  62.     public static void main(String args[]) throws Exception{
  63.         Thread t1 = new ThreadViaClass("t1");
  64.         Thread t2 = new ThreadViaClass("t2");
  65.  
  66.         Thread t3 = new Thread(new ThreadViaInterface("t3"));
  67.        
  68.         // anonymous class with thread name Thread t4
  69.         Thread t4 = new Thread(() ->
  70.             {
  71.                 for(int i=0;i<10;i++){
  72.                     System.out.println("t4");
  73.                     try{
  74.                         Thread.sleep(100);
  75.                     }catch(Exception e){
  76.                         System.out.println(e);
  77.                     }
  78.                }
  79.             }, "Thread t4"
  80.         );
  81.  
  82.         // setting and getting name for easier access
  83.         t1.setName("thread t1");
  84.         System.out.println(t1.getName());
  85.  
  86.         // priorities by default 5, 1- lowest 10-highest
  87.         t1.setPriority(10);
  88.         System.out.println(t1.getPriority());
  89.         System.out.println(t2.getPriority());
  90.  
  91.         // Thread.currentThread(); prints main
  92.         System.out.println(Thread.currentThread().getName());
  93.  
  94.         // start automatically calls Thread.run();
  95.         t1.start();
  96.         t2.start();
  97.         t3.start();
  98.         t4.start();
  99.         // t1 t2 t3 printed in any order as they are running parallely
  100.        
  101.         // isAlive()
  102.         System.out.println(t1.isAlive());
  103.  
  104.         // joining t1 t2 t3 with parent thread (here main)
  105.         t1.join();
  106.         t2.join();
  107.         t3.join();
  108.         t4.join();
  109.  
  110.         // this prints false
  111.         System.out.println(t1.isAlive());                
  112.         // printed at last
  113.         System.out.println("Joined with main thread");
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement