Time-Of-Check-To-Time-Of-Use tahdidini amalga oshirish va himoyalash
Download 284.13 Kb.
|
5-amaliy Ochilov O
- Bu sahifa navigatsiya:
- Toshkent-2023 Amaliy ish - 5 Mavzu
O‘ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI. 5-Amaliy ish Mavzu: Time-Of-Check-To-Time-Of-Use tahdidini amalga oshirish va himoyalash. Bajardi : Usmonov H Tekshirdi : Tursunov O Toshkent-2023 Amaliy ish - 5 Mavzu: Time-Of-Check-To-Time-Of-Use tahdidini amalga oshirish va himoyalash. Ishdan maqsad: Tezkor murojaat sharti tahdidi haqida tushumcha hosil qilish, TOCTOU tahdidi haqida tushumcha hosil qilish, tahdidlarning oldini olish ko‘nikmasini shakllantirish. Topshiriq Java dasturlash tilida sodda dastur yaratish. TOCTOU va Race Conditional tahdidlariga bir donadan misol keltirish va tahdidlarning oldini olish yo‘llarini qo‘llash. 1-rasm Threadlar qanday ishlashi Har bir threadni o’zini ID si mavjud. SHunda Threadlar qay tarzda ishlashini bilib olihsimiz mumkin. public class Main implements Runnable { public void run() { for (int i = 1; i <= 5; i++) { System.out.println("Thread " + Thread.currentThread().getId() + " time: " + System.currentTimeMillis()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } // This code is written by Usmonov Hofizullo 713-19 public static void main(String[] args) { Thread thread1 = new Thread(new Main()); Thread thread2 = new Thread(new Main()); Thread thread3 = new Thread(new Main()); thread1.start(); thread2.start(); thread3.start(); } } 2-rasm, Quyida 2ta threadni qanday parallel ishlashini ko’rishimiz mumkin. public class Main implements Runnable { private String runner; public Main(String runner) { this.runner = runner; }
public void run() { for (int i = 1; i <= 5; i++) { System.out.println(runner + " is at mile " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(runner + " has finished the race!"); } // This code is written by Usmonov Hofizullo 713-19 public static void main(String[] args) { Thread thread1 = new Thread(new Main("Runner 1")); Thread thread2 = new Thread(new Main("Runner 2")); thread1.start(); thread2.start(); } } 3-rasm. Quyida yana bir namuna, Sumcalculator funksiyasida ko’rishimiz mumkin. public class Main { //This code is written by Usmonov Hofizullo 713-19 // Feel free to use public static void main(String[] args) { int[] arr = {1, 5, 3, 9, 7, 2, 4, 8, 6}; SumCalculator calculator1 = new SumCalculator(arr, 0, 3); SumCalculator calculator2 = new SumCalculator(arr, 3, 6); SumCalculator calculator3 = new SumCalculator(arr, 6, 9); Thread thread1 = new Thread(calculator1); Thread thread2 = new Thread(calculator2); Thread thread3 = new Thread(calculator3); thread1.start(); thread2.start(); thread3.start(); try {
thread2.join(); thread3.join(); } catch (InterruptedException e) { e.printStackTrace(); } int sum = calculator1.getSum() + calculator2.getSum() + calculator3.getSum(); System.out.println("Sum: " + sum); } } class SumCalculator implements Runnable { private int[] arr; private int start; private int end; private int sum; public SumCalculator(int[] arr, int start, int end) { this.arr = arr; this.start = start; this.end = end; } public void run() { for (int i = start; i < end; i++) { sum += arr[i]; } } public int getSum() { return sum; } } Download 284.13 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling