Lecturer: Djurayev Oybek Nuruddinovich



Download 285,5 Kb.
bet4/4
Sana19.07.2021
Hajmi285,5 Kb.
#123397
1   2   3   4
Bog'liq
Lecture 1 (1)

Methods of Thread class
  • - public void start()
  • - public void run()
  • - public void sleep(long miliseconds)
  • - public int getPriority()
  • - public int setPriority(int priority)
  • - public String getName()
  • - public void setName(String name)
  • - public Thread currentThread()
  • - public int getId()
  • - public Thread.State getState()
  • - public boolean isAlive()
  • - public void interrupt()
  • - public void suspend()
  • - public void resume()
  • - public void stop()
  • Java Thread Example by extending Thread class
  • class Multi extends Thread{  
  • public void run(){  
  • System.out.println(“thread is running...");  
  • }  
  • public static void main(String args[]){  
  • Multi t1=new Multi();  
  • t1.start();  
  •  }  
  • }  
  • RESULT:
  • thread is running...
  • by implementing Runnable interface
  • class Multi1 implements Runnable{  
  • public void run(){  
  • System.out.println(“thread is running...");  
  • }  
  •  
  • public static void main(String args[]){  
  • Multi1 m1=new Multi1();  
  • Thread t1 =new Thread(m1);  
  • t1.start();  
  •  }  
  • }  
  • RESULT:
  • thread is running...
  • sleep() method
  • class TestSleepMethod1 extends Thread{  
  •  public void run(){  
  •   for(int i=1;i<5;i++){  
  •     try{Thread.sleep(1000);}
  • catch(InterruptedException e){System.out.println(e);}  
  •     System.out.println(i);  
  •   }  
  •  }  
  •  public static void main(String args[]){  
  •   TestSleepMethod1 t1=new TestSleepMethod1();  
  •   TestSleepMethod1 t2=new TestSleepMethod1();  
  •    
  •   t1.start();  
  •   t2.start();  
  •  }  
  • }  
  • sleep() method (RESULT)
  • 1
  • 1
  • 2
  • 2
  • 3
  • 3
  • 4
  • 4
  • call run() method directly instead start()
  • class TestCallRun2 extends Thread{  
  •  public void run(){  
  •   for(int i=1;i<5;i++){  
  •     try{Thread.sleep(1000);}
  • catch(InterruptedException e){System.out.println(e);}  
  •     System.out.println(i);  
  •   }  
  •  }  
  •  public static void main(String args[]){  
  •   TestCallRun2 t1=new TestCallRun2();  
  •   TestCallRun2 t2=new TestCallRun2();  
  •    
  •   t1.run();  
  •   t2.run();  
  •  }  
  • }
  • call run() method directly instead start()
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • Thread Priority
  • class TestMultiPriority1 extends Thread{  
  •  public void run(){  
  •    System.out.println("running thread name is:"+
  • Thread.currentThread().getName());  
  •    System.out.println("running thread priority is:"+
  • Thread.currentThread().getPriority());  
  •   
  •   }  
  •  public static void main(String args[]){  
  •   TestMultiPriority1 m1=new TestMultiPriority1();  
  •   TestMultiPriority1 m2=new TestMultiPriority1();  
  •   m1.setPriority(Thread.MIN_PRIORITY);  
  •   m2.setPriority(Thread.MAX_PRIORITY);  
  •   m1.start();  
  •   m2.start();  
  •    
  •  }  
  • }
  • Thread Priority (RESULT)
  • running thread name is: Thread-1
  • running thread priority is: 10
  • running thread name is: Thread-0
  • running thread priority is: 1
  • Recommended Literature
  • An Introduction to Network Programming with Java. Java 7 Compatible. Jan Graba. 3rd Ed. Springer. 2013
  • Java Network Programming. Elliotte Rusty Harold.-4th ed. Published by O’Reilly Media. 2014.
  • https://www.javatpoint.com/multithreading-in-java

Download 285,5 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish