Dr.Galip AYDIN - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Dr.Galip AYDIN

Description:

Java Threads DR.GAL P AYDIN Java Threads ( Par ac klar ) Multithreading:Program i erisinde ayn anda birden fazla i in yap labilmesi. – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 26
Provided by: webFirat8
Category:

less

Transcript and Presenter's Notes

Title: Dr.Galip AYDIN


1
Java Threads
  • Dr.Galip AYDIN

2
Java Threads (Is Parçaciklari)
  • MultithreadingProgram içerisinde ayni anda
    birden fazla isin yapilabilmesi.
  • Ayni program içerisinde birçok Thread
    çalisabilir.
  • Javada bütün Thread siniflari java.lang.Thread
    class kullanilarak olusturulur ve kontrol edilir.
  • Bu is parçaciklari eszamanli olarak asenkron veya
    senkron olarak çalisabilir.

3
Multithreading vs. Multiprocessing
  • Bagimsiz islemlerle karsilastirildiginda is
    parçaciklari daha hafiftir
  • Is parçaciklari ayni adres alanini paylastiklari
    için veri ve kodlari paylasabilir.
  • Context switching (içerik degistirme) is
    parçaciklarinda islemlere göre daha az pahalidir
  • Is parçaciklari arasi haberlesme islemler arasi
    haberlesmeye göre daha ucuzdur
  • Is parçaciklari farkli görevlerin ayni zaman
    araliginda gerçeklestirilmesine olanak saglarlar

4
(No Transcript)
5
Java Threads
  • thread bir nesne degil bir akis kontrolü
    yöntemidir
  • Sirayla isletilecek bir dizi komut barindirir
  • Thread bir nesnedir
  • void start()
  • Yeni bir Thread olusturup çalisabilir hale
    getirir
  • void run()
  • Yeni Thread hayatina bu metodun içinde baslar

6
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
7
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
8
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
9
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
10
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
11
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
12
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
13
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
14
Thread Olusturulmasi
Object A
Object BThread (extends Thread)
Thread t new BThread() t.start() doMoreStuff
()
BThread() void start() // create
thread void run() doSomething()
15
Is parçaciklarinin olusturulmasi
  • Javada bir is parçacigi olusturmak için iki
    yöntem kullanilir
  • Runnable interface kullanmak (java.lang.Runnable)
  • Thread sinifini extend etmek (java.lang.Thread)

16
Runnable Interface
  • Thread nesnesine yardimci bir nesnedir
  • Thread nesnesinin run() metodu Runnable
    nesnesinin run() metodunu çagirir
  • Threadin kalitima bagli olmadan, herhangi bir
    nesnenin içinde çalismasini saglar

17
Runnable Interface
  • The Runnable Interface Signature
  • public interface Runnable
  • void run()
  • 1. Herhang bir sinif Runnable Interfaceini
    thread tarafindan çalistirilacak olan run()
    metodunu saglayarak implement eder.
  • 2. Thread sinifinin bir nesnesi, kurucu metoduna
    bir Runnable nesnesi geçilerek olusturulur. Simdi
    Thread nesnesinin run() metodunu implement eden
    bir Runnable nesnesi var.
  • 3. Önceki adimda olusturulan Thread nesnesinin
    start() metodu uyarilir. Bu metod yeni bir thread
    baslatilir baslatilmaz kontrolü döndürür.
  • 4. run() metodu biter bitmez thread biter.

18
Not Runnable
  • Bir thread "Not Runnable" durumuna asagidaki dört
    durumdan birisi olusunca girer
  • suspend() metod u çagrilinca
  • sleep() metodu çagrilinca
  • Thread wait() metodu kullanarak bir sart
    degiskenini beklemek için kullaniyorsa
  • I/O için blok olmussa.

19
Thread Lifecycle
Active
sleep(500)
Blocked
wake up
Born
JVM
start()
suspend()
Runnable
resume()
stop()
wait
stop()
notify
Dead
block on I/O
I/O available
20
public class RunnableOrnek public static
void main(String args) Thread thread1
new Thread(new RunnableThread(), "thread1")
Thread thread2 new Thread(new
RunnableThread(), "thread2")
RunnableThread thread3 new RunnableThread("threa
d3") //Start the threads
thread1.start() thread2.start()
try //delay for one second
Thread.currentThread().sleep(1000)
catch (InterruptedException e)
//Display info about the main thread
System.out.println("MAIN " Thread.currentThread(
))
  • class RunnableThread implements Runnable
  • Thread runner
  • public RunnableThread()
  • public RunnableThread(String threadName)
  • runner new Thread(this, threadName)
  • System.out.println("RUNNER "
    runner.getName())
  • runner.start()
  • public void run()
  • System.out.println("INFO " Thread.currentThrea
    d())

21
RUNNER thread3 INFO Threadthread3,5,main INFO
Threadthread2,5,main INFO Threadthread1,5,main
MAIN Threadmain,5,main
22
2. Yöntem Thread Sinifinin Extend Edilmesi
  • Thread sinifini extend eden bir sinif, Thread
    sinifinin run() metodunu yeniden olusturarak
    yürütülecek kodu tanimlar.
  • Bu sinif kurucu metodunda is parçacigini
    baslatmak için Thread kurucu metodunu super()
    kullanarak uyarabilir.
  • Thread sinifindan miras alinan start() metodu is
    parçacigini baslatacak olan sinifta çagrilir.

23
  • class ThreadClass extends Thread
  • private long basla
  • public ThreadClass(String isim)
  • super(isim)
  • public void yaz()
  • try
  • for (int i 0 i lt 5 i)
  • sleep(500)
  • long simdi System.currentTimeMillis(
    )
  • Calendar c Calendar.getInstance()
  • c.setTimeInMillis(simdi)
  • System.out.println(getName() "
    " c.getTime().toString())
  • //System.out.println(getName() " "
    (simdi-basla))
  • catch (Exception ex)
  • ex.printStackTrace()
  • //System.out.println(System.currentTimeMil
    lis())

24
import java.util.Calendar public class
ThreadOrnek public static void
main(String args) long basla
System.currentTimeMillis() ThreadClass t1
new ThreadClass("Thread 1") ThreadClass t2
new ThreadClass("Thread 2") ThreadClass t3
new ThreadClass("Thread 3") t1.start()
t2.start() t3.start()
System.out.println("----------------") long
son System.currentTimeMillis()
System.out.println("GECEN ZAMAN "
(son-basla))
25
GECEN ZAMAN 0 Thread 1 Mon Mar 02 125136 EET
2009 Thread 2 Mon Mar 02 125136 EET 2009 Thread
3 Mon Mar 02 125136 EET 2009 Thread 1 Mon Mar
02 125136 EET 2009 Thread 2 Mon Mar 02 125136
EET 2009 Thread 3 Mon Mar 02 125136 EET
2009 Thread 1 Mon Mar 02 125137 EET 2009 Thread
2 Mon Mar 02 125137 EET 2009 Thread 3 Mon Mar
02 125137 EET 2009 Thread 1 Mon Mar 02 125137
EET 2009 Thread 2 Mon Mar 02 125137 EET
2009 Thread 3 Mon Mar 02 125137 EET 2009 Thread
1 Mon Mar 02 125138 EET 2009 Thread 1
EXITING Thread 2 Mon Mar 02 125138 EET
2009 Thread 2 EXITING Thread 3 Mon Mar 02
125138 EET 2009 Thread 3 EXITING
Write a Comment
User Comments (0)
About PowerShow.com