Your SlideShare is downloading. ×
0
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
18   concurrency
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

18 concurrency

754

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
754
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
26
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Thread
    DhrubojyotiKayal
  • 2. Independent, concurrent paths of execution through a program
    lightweight processes
    Allow multiple activities to coexist within a single process
    Java is the first mainstream programming language to explicitly include threading within the language itself, rather than treating threading as a facility of the underlying operating system, paving way for rapid growth of Java as a language of choice for writing servers.
    Primer
  • 3. Every Java program has at least one thread -- the main thread. When a Java program starts, the JVM creates the main thread and calls the program's main() method within that thread
    The JVM also creates other threads that are mostly invisible to programmers
    Threads associated with garbage collection, object finalization, and other JVM housekeeping tasks
    Threads everywhere
  • 4. More responsive UI
    A Swing event handler which does a long running process, it will hang and blur the entire UI and will not manage to handle any other events. Trigger alternate threads and hand over the long running task
    Take advantage of multi-processor systems
    Systems with multiple processors are common these days even in desktops supported by all OSes. Threads help schedulers make optimal usage of these processors by scheduling tasks to idle processor
    Why use threads?
  • 5. Simplicity in design
    What will happen to web applications if they handle one request at a time?
    Background processing
    How will you poll a FTP directory, look for an incoming file and trigger off some processing
    Why use threads?
  • 6. Extend Thread class
    public class MyThread extends Thread {
    @Override
    public void run() {
    System.out.println("Hey I am running in a thread");
    }
    public static void main(String a[]) {
    MyThread t = new MyThread();
    t.start();
    }
    }
    Your first thread
  • 7. public class MyRunnable implements Runnable {
    @Override
    public void run() {
    System.out.println("Hey I am runnable thread");
    }
    public static void main(String a[]) {
    MyRunnable t = new MyRunnable();
    Thread t2 = new Thread(t);
    t2.start();
    }
    }
    Implementing Runnable
  • 8. public class MyThread extends Thread {
    private int max;
    private int min;
    public MyThread(int min , int max) {
    this.max = max;
    this.min = min;
    }
    @Override
    public void run() {
    int counter = min;
    while(counter <= max) {
    System.out.println(Thread.currentThread().getName() + " : " + counter );
    counter++;
    }
    }
    public static void main(String a[]) {
    MyThread t1 = new MyThread(1,5);
    MyThread t2 = new MyThread(6,10);
    MyThread t3 = new MyThread(11,15);
    t1.start();
    t2.start();
    t3.start();
    }
    }
    Thread in Action
  • 9. public class SharedData{
    private String data = "HAPPY";
    public void accessData(){
    for(inti =0; i<20; i++)
    {
    data = data + " " + i + " ";
    }
    System.out.println(data);
    }
    }
    Shared data
  • 10. public class MyThread extends Thread{
    private SharedData data;
    public MyThread(SharedData data){
    this.data = data;
    }
    public void run(){
    data.accessData();
    }
    }
    Shared data
  • 11. public class ThreadSynchronizationTest {
    public static void main(String[] args) { SharedData data = new SharedData(); MyThread one = new MyThread(data); one.start(); MyThread two = new MyThread(data); two.start();
    }
    }
    And the output is??
    Shared data test
  • 12. public synchronized void accessData(){
    for(inti =0; i<20; i++)
    {
    data = data + " " + i + " ";
    }
    System.out.println(data);
    }
    Synchronize
  • 13. synchronized (this)
    for(inti =0; i<20; i++)
    {
    data = data + " " + i + " ";
    }
    System.out.println(data);
    }
    Synchronize block
  • 14. Q&A

×