Java was one of the first mainstream programming languages to include threading support within the language itself. This paved the way for Java to become widely used for writing server applications. Every Java program has at least one thread - the main thread. Additional threads are created by the JVM for garbage collection and other tasks. Threads allow programs to take advantage of multi-processor systems and provide more responsive user interfaces by handling long-running processes in background threads.
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