|
| 1 | +import java.util.Scanner; |
| 2 | + |
| 3 | +class MyThread1 extends Thread { |
| 4 | + @Override |
| 5 | + public void run() { |
| 6 | + // System.out.println("MyThread1 run() function"); |
| 7 | + |
| 8 | + // Scanner sc = new Scanner(System.in); |
| 9 | + // int x = sc.nextInt(); // Blocking I/O operation |
| 10 | + |
| 11 | + for (int i = 0; i < 1; i++) { |
| 12 | + System.out.println("MyThread1 run() function"); |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +class MyThread2 implements Runnable { |
| 18 | + public void run() { |
| 19 | + Thread.currentThread().setPriority(6); |
| 20 | + System.out.println(Thread.currentThread().getPriority()); |
| 21 | + Thread.currentThread().setPriority(7); |
| 22 | + System.out.println(Thread.currentThread().getPriority()); |
| 23 | + for (int i = 0; i < 3; i++) { |
| 24 | + System.out.println("MyThread2 run() function"); |
| 25 | + } |
| 26 | + Thread.currentThread().setPriority(2); |
| 27 | + System.out.println("MyThread2 run() function"); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +public class Multithreading { |
| 32 | + public static void main(String[] args) { |
| 33 | + MyThread1 t1 = new MyThread1(); // Newborn state |
| 34 | + t1.setPriority(2); |
| 35 | + t1.start(); // Ready state |
| 36 | + |
| 37 | + // Scanner sc = new Scanner(System.in); |
| 38 | + // int x = sc.nextInt(); // Blocking I/O operation |
| 39 | + |
| 40 | + MyThread2 t = new MyThread2(); |
| 41 | + Thread t2 = new Thread(t); // Newborn state |
| 42 | + t2.setPriority(8); |
| 43 | + t2.start(); // Ready state |
| 44 | + |
| 45 | + System.out.println(t1.getName()); |
| 46 | + System.out.println(t2.getName()); |
| 47 | + |
| 48 | + t1.setName("Thread of MyThread1"); |
| 49 | + t2.setName("Thread of MyThread2"); |
| 50 | + |
| 51 | + System.out.println(t1.getName()); |
| 52 | + System.out.println(t2.getName()); |
| 53 | + System.out.println(Thread.currentThread().getName()); |
| 54 | + |
| 55 | + System.out.println(t1.getId()); |
| 56 | + System.out.println(t2.getId()); |
| 57 | + System.out.println(Thread.currentThread().getId()); |
| 58 | + |
| 59 | + // try { |
| 60 | + // Thread.sleep(10); |
| 61 | + // t1.join(); |
| 62 | + // t2.join(1000); |
| 63 | + // } catch (InterruptedException e) { |
| 64 | + // System.out.println(e); |
| 65 | + // } |
| 66 | + |
| 67 | + System.out.println("Main function terminating"); // Blocking I/O operation |
| 68 | + } |
| 69 | +} |
0 commit comments