Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. write an essay about what is Multithreading, define some cases and select
- appropiate Multithreading scenario to solve those case, explain why.
- Multithreading adalah fitur java yang memungkinkan eksekusi bersamaan
- dua atau lebih bagian dari program untuk pemanfaatan CPU secara maksimal.
- Setiap bagian dari program tersebut disebut utas.
- Jadi utas adalah proses ringan dalam suatu proses.
- Thread dapat dibuat dengan menggunakan dua mekanisme:
- 1. Memperluas kelas Thread
- 2. Menerapkan
- 2. write an essay about race condition and deadlock condition. try to suggest how to avoid.
- saya membuat kasus try dengan menngunakan fungsi Thread dan Runable dimana kedua fungsi tersebut hasilnya sama untuk pengecekan suatu akses yang sedang berjalan.
- 3. write a simple code to show Multithreading in action by implementing you cases about race and deadlock condition above.
- 1. contoh Thread
- package Multithreading;
- class MultitherdingDemo extends Thread
- {
- public void run()
- {
- try
- {
- // Displaying the thread that is running
- System.out.println ("Thread " +
- Thread.currentThread().getId() +
- " is running");
- }
- catch (Exception e)
- {
- // Throwing an exception
- System.out.println ("Exception is caught");
- }
- }
- }
- package Multithreading;
- public class Multitheread
- {
- public static void main(String[] args)
- {
- int n = 8; // Number of threads
- for (int i=0; i<8; i++)
- {
- MultitherdingDemo object = new MultitherdingDemo();
- object.start();
- }
- }
- }
- 2. contoh Runnable
- package Multi;
- class MultiDemo implements Runnable
- {
- @Override
- public void run()
- {
- try
- {
- System.out.println("Theread "+Thread.currentThread().getId()+" is runing");
- }
- catch (Exception e)
- {
- System.out.println("Exception is caught");
- }
- }
- }
- package Multi;
- public class test
- {
- public static void main(String[] args)
- {
- int n = 8;
- for(int i = 0; i < 10; i++)
- {
- Thread object = new Thread(new MultiDemo());
- object.start();
- }
- }
- }
Add Comment
Please, Sign In to add comment