Threads & Concurrency Control
Learning Objective
Threads & Concurrency ControlOrientation Video
Introduction
A thread is a program's path of execution. Most programs written today run as a single thread, causing problems when multiple events or actions need to occur at the same time. Let's say, for example, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this.Threads can be created in two ways:
The first method of creating a thread is to simply extend from the Thread class. Do this only if the class you need executed as a thread does not ever need to be extended from another class. The Thread class is defined in the package java.lang, which needs to be imported so that our classes are aware of its definition.
import java.lang.*; public class Counter extends Thread { public void run() { .... } }The above example creates a new class Counter that extends the Thread class and overrides the Thread.run() method for its own implementation. The run() method is where all the work of the Counter class thread is done. The same class can be created by implementing Runnable:
import java.lang.*; public class Counter implements Runnable { Thread T; public void run() { .... } }
Here, the abstract run() method is defined in the Runnable interface and is being implemented. Note that we have an instance of the Thread class as a variable of the Counter class. The only difference between the two methods is that by implementing Runnable, there is greater flexibility in the creation of the class Counter. In the above example, the opportunity still exists to extend the Counter class, if needed. The majority of classes created that need to be run as a thread will implement Runnable since they probably are extending some other functionality from another class.
Resources
Books- Java Complete Reference
- Thinking in Java
//serialization
import java.io.*; class Inter implements Serializable { int x; } class Outer implements Serializable { int y; Inter in; public Outer(Inter in,int y) { this.in=in; this.y=y; } private void writeObject(ObjectOutputStream os) { } } class man { public static void main(String args[])throws IOException,ClassNotFoundException { Inter in=new Inter(); in.x=10; Outer out=new Outer(in,12); ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("test.txt")); System.out.println(out.in.x+" "+out.y); os.writeObject(out); os.close(); } } class min { public static void main(String args[])throws IOException,ClassNotFoundException { Outer out; ObjectInputStream is=new ObjectInputStream(new FileInputStream("test.txt")); out=(Outer)is.readObject(); System.out.println(out.in.x+" "+out.y); is.close(); } }
//Synchronization
import java.util.*; class Mylist { static Vector v=new Vector(); public static void add(String name) { v.add(name); } public static String remove() { Class cls=null; try{ cls=Class.forName("Mylist"); }catch(ClassNotFoundException e){} synchronized(cls) { if(v.size()>0) return (String)v.remove(0); else return null; } } } class Demo { public static void main(String args[]) { final Mylist my1=new Mylist(); final Mylist my2=new Mylist(); my1.add("threads"); class Mythread1 extends Thread { public void run() { System.out.println(my1.remove()); } } class Mythread2 extends Thread { public void run() { System.out.println(my2.remove()); } } Mythread1 t1=new Mythread1(); Mythread2 t2=new Mythread2(); t1.start(); t2.start(); } }//Threads
class HiLoPri { public static void main(String args[]) { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Clicker Hi = new Clicker(Thread.NORM_PRIORITY+2); Clicker Lo = new Clicker(Thread.NORM_PRIORITY-2); Lo.start(); Hi.start(); try { Thread.sleep(100); } catch (Exception e) { } Lo.stop(); Hi.stop(); System.out.println(Lo.click + " vs. " + Hi.click); } } class Clicker implements Runnable { int click = 0; private Thread t; private boolean running = true; public Clicker(int p) { t = new Thread(this); t.setPriority(p); } public void run() { while(running) click++; } public void start() { t.start(); } public void stop() { running = false; } }Threads - Java Complete Refrence 5th Edition 11th Chapter
Tasks
In this task you will be simulating a System Command to recursively list the contents of a given directory. ls in Linux and dir in windows list the contents of a particular directory.Your program should do the following:
1.) In the main part of your program, you will take the directory path as input at the command line and start listing the contents of the directory using threads.
2.) If the Entity is a Directory, print the name of the directory and then create a new thread that takes this directory as input and start working as the main function.
3.) If the Entity is a file, just print the file name.
Hint
Create a seperate thread for each directory and use it for displaying its contents.
Use methods of java.io.File class.
Sample Output
D:\task\java DirectoryListing
Enter the Directory along with the path:C:\Task
The directories and files in the Task folder are:
Directory:Minitask1
File:task1.java
File:task2.java
Directory:Minitask2
File:task1.java
File:task2.java
Comments
Post a Comment