Rahmadnet

Multithreading

Jan 29th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. 1.  write an essay about what is Multithreading, define some cases and select
  2.     appropiate Multithreading scenario to solve those case, explain why.
  3.     Multithreading adalah fitur java yang memungkinkan eksekusi bersamaan
  4.     dua atau lebih bagian dari program untuk pemanfaatan CPU secara maksimal.
  5.     Setiap bagian dari program tersebut disebut utas.
  6.     Jadi utas adalah proses ringan dalam suatu proses.
  7.  
  8.     Thread dapat dibuat dengan menggunakan dua mekanisme:
  9.     1. Memperluas kelas Thread
  10.     2. Menerapkan
  11. 2. write an essay about race condition and deadlock condition. try to suggest how to avoid.
  12.     saya membuat kasus try dengan menngunakan fungsi Thread dan Runable dimana kedua fungsi tersebut hasilnya sama untuk pengecekan suatu akses yang sedang berjalan.
  13. 3. write a simple code to show Multithreading in action by implementing you cases about race and deadlock condition above.
  14.  
  15.     1. contoh Thread
  16.  
  17.     package Multithreading;
  18.  
  19. class MultitherdingDemo extends Thread
  20. {
  21.     public void run()
  22.     {
  23.         try
  24.         {
  25.             // Displaying the thread that is running
  26.             System.out.println ("Thread " +
  27.                   Thread.currentThread().getId() +
  28.                   " is running");
  29.  
  30.         }
  31.         catch (Exception e)
  32.         {
  33.             // Throwing an exception
  34.             System.out.println ("Exception is caught");
  35.         }
  36.     }
  37.    
  38. }
  39.  
  40. package Multithreading;
  41.  
  42. public class Multitheread
  43. {
  44.     public static void main(String[] args)
  45.     {
  46.         int n = 8; // Number of threads
  47.         for (int i=0; i<8; i++)
  48.         {
  49.             MultitherdingDemo object = new MultitherdingDemo();
  50.             object.start();
  51.         }
  52.     }
  53. }
  54.  
  55.     2. contoh Runnable
  56.  
  57.     package Multi;
  58.  
  59. class MultiDemo implements Runnable
  60. {
  61.  
  62.     @Override
  63.     public void run()
  64.     {
  65.         try
  66.         {
  67.             System.out.println("Theread "+Thread.currentThread().getId()+" is runing");
  68.         }
  69.         catch (Exception e)
  70.         {
  71.             System.out.println("Exception is caught");
  72.         }
  73.     }
  74.  
  75. }
  76.  
  77. package Multi;
  78.  
  79. public class test
  80. {
  81.     public static void main(String[] args)
  82.     {
  83.         int n = 8;
  84.         for(int i = 0; i < 10; i++)
  85.         {
  86.             Thread object = new Thread(new MultiDemo());
  87.             object.start();
  88.         }
  89.     }
  90. }
Add Comment
Please, Sign In to add comment