You are on page 1of 32

Java Programming

UNIT-4
Multi Threading, I/O Streams

Topics covered in this unit:


• Multithreading:
 Introduction,
 Thread life cycle,
 Creation of threads,
 Thread priorities,
 Thread synchronization,
 Communication between threads.
• Files and Streams:
 Reading data from files and writing data to files,
 Random access file.

Types of Multi-tasking:
• You expect programs to do many things at once, known as multi-tasking.
– Respond to a Mouse Click
– Spell Check Spelling
– Print
– Do Network Communications
– Do I/O
• Multi-tasking programs should have high performance, may utilize multiprocessors.
– There are two distinct types of multitasking: Process-based and Thread-based.
• The Program in execution is defined as Process. Thus, the process based multi tasking is the
feature that allows your computer to run two or more programs concurrently.
• In the thread-based multitasking environment, the thread is the smallest unit of dispatchable
code.
• This means that the single program can contain two or more parts, each part of the program is
executed in a separate path, called Thread.
• Ex: the text editor can be formatting the text and also printing the text in two different threads.
• Although the Java programs make use of the process-based multi tasking environments, but the
process-based multi tasking is not under the control of java, Where as the thread-based
multitasking is under the control of Java.

Process-Based Multitasking Thread-Based Multitasking


This deals with "Big Picture" This deals with Details
These are Heavyweight tasks These are Lightweight tasks
Inter-process communication is expensive and Inter-Thread communication is inexpensive.
limited
Context switching from one process to another Context switching is low cost in terms of
is costly in terms of memory memory, because they run on the same address
space
This is not under the control of Java This is controlled by Java

Advantage of the Multithreading


• It enables you to write very efficient programs that maximizes the CPU utilization and reduces
the idle time.
• Most I/O devices such as network ports, disk drives or the keyboard are much slower than CPU
• A program will spend much of it time just send and receive the information to or from the
devices, which in turn wastes the CPU valuable time.
• By using the multithreading, your program can perform another task during this idle time.
• For example, while one part of the program is sending a file over the internet, another part can
read the input from the keyboard, while other part can buffer the next block to send.
• It is possible to run two or more threads in multiprocessor or multi core systems simultaneously.

What is a Thread?

• Threads are called lightweight processes.


• A thread (or thread of control) is a sequence of executable statements within a program in a
separate path.
• Threads are spawned from Processes.
• So all the threads created by one particular process can share the process's resources (memory,
file handlers, etc).
• Threads allow multiple program flows to coexist within the process.
• Even though threads share process-wide resources (memory, file handlers), but each thread has
its own Program Counter, Stack and local variables.
• Benefits
– using multiple processors
– Handling asynchronous events
– Optimal utilization of processor and resources.
• Risks
– Sharing common data(Synchronization)
– Deadlocks
– Context switching
Concurrent Execution of Threads (Multi threading):

• Multitasking is the technique of concurrently executing several tasks within a program.


• On a sequential computer the threads share the single CPU (Central Processing Unit).
• Time slicing -- each thread alternatively gets a slice (quantum) of the CPU's time.
Sharing resources (Single threaded and multi-threaded):

A Java Program to explain Single Thread Model:

SingleThreadModel.java

class MyProgram
{
public void run()
{
for(int i=5; i>0;i--)
{
System.out.println("from child thread : " + i);
try {
Thread.sleep(1000);
}catch(InterruptedException ie) {}
}
}
}
class SingleThreadModel
{
public static void main(String args[] )
{
MyProgram mp1 = new MyProgram();
mp1.run();
for(int j=5; j>0;j--)
{
System.out.println("from main thread : " + j);
try {
Thread.sleep(1000);
}catch(InterruptedException ie) {}
}
}
}

Output:

A Java Program to explain Multi Thread Model:

MultiThreadModel.java

class MyProgram extends Thread


{
public void run()
{
for(int i=5; i>0;i--)
{
System.out.println("from child thread : " + i);
try {
Thread.sleep(1000);
}catch(InterruptedException ie) {}
}
}
}
class MultiThreadModel
{
public static void main(String args[] )
{
MyProgram mp1 = new MyProgram();
mp1.start();
for(int j=5; j>0;j--)
{
System.out.println("from main thread : " + j);
try {
Thread.sleep(1000);
}catch(InterruptedException ie) {}
}
}
}

Output:

Creation of Thread

Creating the threads in the Java is simple. The threads can be implemented in the form of object
that contains a method "run()". The "run()" method is the heart and soul of any thread. It makes up the
entire body of the thread and is the only method in which the thread behavior can be implemented.
There are two ways to create thread.
1. Declare a class that extends the Thread class and override the run() method.
2. Declare a class that implements the Runnable interface which contains the run() method .

1. Extending the thread class


We can make our thread by extending the Thread class of java.lang.Thread class. This gives us
access to all the methods of the Thread. It includes the following steps:
I. Declare the class as Extending the java.lang.Thread class.
II. Override the "run()" method that is responsible for running the thread.
III. Create a thread and call the "start()" method to instantiate the Thread Execution.

Declaring the class


TheThread class can be declared as follows:
class MyThread extends Thread
{
-----------------------
----------------------
----------------------
----------------------
}

Overriding the method run()


The run() is the method of the Thread. We can override this as follows:
public void run()
{
----------------
----------------
----------------
}
Starting the new Thread
To actually to create and run an instance of the thread class, we must write the following:

MyThread a=new MyThread(); // creating the Thread


a.start(); // Starting the Thread

Example program:
class NewThread extends Thread {
NewThread() {
super("Demo Thread");
System.out.println("child thread: " + this);
start();
}
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("child Thread: " + i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}

class ExtendThreadDemo
{
public static void main(String[] args)
{
NewThread t1 = new NewThread();
try
{
for(int i=5;i>0;i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interupted");
}
System.out.println("Main thread exiting.");
}
}
Output:
2. Implementing the Runnable Interface
The Runnable interface contains the run() method that is required for implementing the threads
in our program. To do this we must perform the following steps:
I. Declare a class as implementing the Runnable interface
II. Implement the run() method
III. Create a Thread by defining an object that is instantiated from this "runnable" class as the target
of the thread
IV. Call the thread's start() method to run the thread.

Example program:
class NewThread implements Runnable
{
Thread t;
NewThread() {
t = new Thread(this,"Demo Thread");
System.out.println("child thread: " + t);
t.start();
}
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("child Thread: " + i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}

class RunnableDemo
{
public static void main(String[] args)
{
NewThread t1 = new NewThread();
try
{
for(int i=5;i>0;i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}catch (InterruptedException e) {
System.out.println("Main thread interupted");
}
System.out.println("Main thread exiting.");
}
}
Output:

Thread Life Cycle and States:


A thread in Java at any point of time exists in any one of the following states. A thread lies only in one of
the shown states at any instant:
1. New
2. Runnable
3. Running
4. Not-Runnable
i. Blocked
ii. Waiting
iii. Timed Waiting
5. Dead
1. New Thread: When a new thread is created, it is in the new-born state. The thread object in the
new state, is not allowed for execution with sharing the processor cycles.
2. Runnable State: After calling start() method, the thread object enters to the Runnable(Ready to
Run) state. All the thread objects in runnable state are added to the job scheduler queue,
maintained by operating system. The thread objects in the scheduler queue are eligible to share
processor cycles for execution. The operating system uses some algorithm ( ex: round robin
algorithm) to allocate the processor cycles for each thread object in the scheduler queue.
3. Running state: A multi-threaded program allocates a fixed amount of time to each individual
thread. Each and every thread runs for a short while and then pauses and relinquishes the CPU to
another thread, so that other threads can get a chance to run. When this happens, all such
threads that are ready to run, waiting for the CPU and the currently running thread lies in
runnable state. You may also call yield() method on running thread to send the thread into
runnable state.
4. Dead State: Once the thread finished executing, it’s state is changed to Dead and it’s considered
to be not alive. Once the thread object is dead, then we can’t re-invoke for execution.
5. Not-Runnable state: The running thread can be interrupted due to different reasons for
temporary. This temporary idle state is known as Not-Runnable state or blocked state. The
reasons for blocked state are:
 Calling sleep() method
 Calling wait() method until notification.
 Calling suspend() method ( java old versions).
 Calling join() method by child thread.
 Blocked for I/O operations from user.
 Lock acquisition.

For example, when a thread is waiting for I/O to complete, it lies in the blocked state. It’s the
responsibility of the thread scheduler to reactivate and schedule a blocked/waiting thread. A thread in
this state cannot continue its execution any further until it is moved to runnable state. Any thread in
these states do not consume any CPU cycle.
If a currently running thread is moved to blocked/waiting state, another thread in the runnable
state is scheduled by the thread scheduler to run. It is the responsibility of thread scheduler to
determine which thread to run.
When the sleep time is over, or any other thread called notify() method, or resume() method
and if I/O operation is completed the thread in Not-Runnable state is transitioned to Runnable state.

Thread class methods:

• void start() – new born thread enters into runnable state


• void run() – entry point for the thread.
• Thread.currentThread() – returns reference of the currently executing thread.
• String getName(), void setName() – are used to manage the name of the thread
• void setPriority(), int getPriority() - are used to access/modify the priority of the thread.
• getState() – returns the state of the thread.
• Boolean isAlive() – returns Boolean value if thread object is alive or not.
• final void join() – Waits until another thread to terminate.
• void run() – entry point for the thread.
• void start() – the new born thread enters into runnable state.
• void setDaemon (boolean) – setting the thread as background thread to serve another thread.
• void interrupt(), void stop(), void destroy() – to stop the thread.
• void yield() – turns the running thread into runnable.
• interrupt():  Interrupts this thread and the thread is terminated to dead state.
• sleep(long millis): Causes the currently executing thread to sleep (temporarily cease execution)
for the specified number of milliseconds.

Using 1) isAlive() and 2) join() methods:

It is often very important to know which thread is ended. This helps to prevent the main from
terminating before the child Thread is terminating. To address this problem "Thread" class provides two
methods: 1) Thread.isAlive() 2) Thread.join().
The "isAlive()" method returns the either "TRUE" or "FALSE" . It returns "TRUE" if the thread is
alive, returns "FALSE" otherwise.
While isAlive( ) is occasionally useful, the method that you will more commonly use to wait for a
thread to finish is called join( ), shown here:
final void join( ) throws InterruptedException
This method waits until the thread on which it is called terminates. Its name comes from the
concept of the calling thread waiting until the specified thread joins it. Additional forms of join( ) allow
you to specify a maximum amount of time that you want to wait for the specified thread to terminate.
Program:
class NewThread extends Thread {
NewThread(String threadname) {
super(threadname);
System.out.println("New thread : " + this);
start();
}
public void run(){
try
{
for(int i=5; i>0;i--) {
System.out.println(getName() + " : " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e){
System.out.println(getName() + " interrupted.");
}
System.out.println(getName() + " exiting...");
}
}
class JoinDemo
{
public static void main(String[] args) {
NewThread ob1 = new NewThread("One");
System.out.println("Thread One is alive : " + ob1.isAlive());
try
{
System.out.println("Waiting for child thread to finish....");
ob1.join();
}
catch (InterruptedException e) {
System.out.println("main thread Interrupted");
}
System.out.println("Thread One is alive : " + ob1.isAlive());
System.out.println("Main Thread exiting....");
}
}

Output:

Thread priorities :

 Each thread has a priority.


 Priorities are represented by a number between 1 and 10.
 In most cases, thread scheduler schedules the threads according to their priority (known as
preemptive scheduling).
 But it is not guaranteed because it depends on JVM specification that which scheduling it
chooses.
 Three constants defined in Thread class:
o public static int MIN_PRIORITY
o public static int NORM_PRIORITY
o public static int MAX_PRIORITY
 Default priority of a thread is 5 (NORM_PRIORITY).
 The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.

Example for priority of a Thread:

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();
}

Output:
running thread name is: Thread-0
running thread priority is: 10
running thread name is: Thread-1
running thread priority is: 1

Daemon thread:
 A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but
the thread is still running.
 An example for a daemon thread is the garbage collection.
 You can use the setDaemon(boolean) method to change the Thread daemon properties before the
thread starts.
 Daemon threads acts like service providers for other threads running in the same process.
 Daemon threads will be terminated by the JVM when there are none of the other threads running, it
includes main thread of execution as well.
 To determine if a thread is a daemon thread, use the accessor method isDaemon().

Program to demonstrate Daemon thread:


public class DaemonThreadDemo extends Thread
{
public static void main(String[] args)
{
System.out.println("Main Method Entry");
DaemonThreadDemo t = new DaemonThreadDemo();
t.setDaemon(true);
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException x) { }
System.out.println("Main Method Exit");
}
public void run() {
System.out.println("run() Method Entry");
try {
System.out.println( "In run Method: currentThread() is "
+ Thread.currentThread());
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException x) { }
System.out.println("In run method.." + Thread.currentThread());
}
} finally {
System.out.println("run() Method Exit");
}
}
}
Output:

Synchronization

When two or more threads need access to a shared resource, they need some way to ensure
that the resource will be used by only one thread at a time. That particular code segment which contains
code to access or update the shared resource is known as critical section. Thread synchronization is
defined as a mechanism which ensures that two or more concurrent processes or threads do not
simultaneously execute the critical section.
Two or more threads executing simultaneously are called concurrent threads. Critical section is
part of the program, which contains code to access or modify the shared resource, and has to be
executed as an atomic action. Critical section is to be executed by only one thread at a time in
synchronization.
So synchronization mechanism takes care not to allow other threads to execute the critical
section.
Key to synchronization is the concept of the monitor (also called a semaphore). A monitor is an
object that is used as a mutually exclusive lock, or mutex. Only one thread can own a monitor at a given
time.
When a thread acquires a lock, it is said to have entered the monitor. All other threads
attempting to enter the locked monitor will be suspended until the first thread exits the monitor.
These other threads are said to be waiting for the monitor. A thread that owns a monitor can
reenter the same monitor if it so desires.
Example for Shared resource and critical section:
An Account is operating by two operations by two threads.
a) Depositing 1000 rs. By thread-1
b) Withdrawing 500 rs. By thread-2
Initial balance is Rs. 700/- after completing two operations the final effected balance is rs. 1200/-
So rs. 1200/- is consistent value after two operations.

But multithreading performs concurrent operations, so it leads to some side effects.

Side effect of multi threading on shared resource.

After final updating by thread-2, the value in balance variable is Rs. 200/- , which is inconstant.
There are two types of thread synchronization mutual exclusive and inter-thread communication.
1. Mutual Exclusive
a) Synchronized method.
b) Synchronized block.
2. Cooperation (Inter-thread communication in java)
a) synchronized method:
class A
{
………
……..
synchronized void method1() {
// critical section code
}
………
………
}
• If you declare any method as synchronized, it is known as synchronized method.
• Synchronized method is used to lock an object for any shared resource.
• When a thread invokes a synchronized method, it automatically acquires the lock for that object and
releases it when the thread completes its task.

b) synchronized block:
• You do not have to synchronize a whole method. Sometimes it is preferable to synchronize only part
of a method. Java synchronized blocks inside methods makes this possible.
• Here is a synchronized block of Java code inside an unsynchronized Java method:

public void add(int value){


synchronized(obj){
obj.method1();
}
}
Importance of Synchronization:

Synchronization is built around an internal entity known as the lock or monitor. Every object has
an lock associated with it. By convention, a thread that needs consistent access to an object's fields has
to acquire the object's lock before accessing them, and then release the lock when it's done with them.
So Synchronization avoids inconsistent values in the shared resources in concurrency control.
Disadvantages of Synchronization:

1) Synchronization mechanism reduces the performance of the program.


2) Another thread should wait until current thread has to complete its work.
3) Leads to starvation.
Starvation is the name given to the indefinite postponement of a process because it requires
some resource before it can run, but the resource, though available for allocation, is never allocated
to this process.
4) Leads to Deadlocks.
Deadlock can occur in a situation when a thread is waiting for an object lock, that is acquired by
another thread and second thread is waiting for an object lock that is acquired by first thread.

Program to demonstrate synchronized method:

class CallMe
{
/* synchronized */ void call(String msg) {
System.out.print("["+ msg);
try{
Thread.sleep(1000);
}catch(InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable
{
String msg;
CallMe target;
Thread t;
public Caller(CallMe targ,String s)
{
target = targ;
msg = s;
t= new Thread(this);
t.start();
}
public void run() {
target.call(msg);
}
}
class SyncDemo
{
public static void main(String[] args)
{
CallMe target = new CallMe();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}

Output:

Case 1: method is not synchronized :

Case 2 : method is synchronized: ( remove comment around the word "synchronized" in line no:2 of
program.)

Inter-Thread Communication:

• To control Multi thread execution, and avoid conflicts, java provided a mechanism called Inter
Thread Communication.
• When conflict is occurred, the current thread goes into wait state, using wait() method, and it
provide chance to execute other thread.
• After conflict is solved, the second thread informed with notify() method, now first thread
resumed into running state.
• This process is called Inter Thread Communication.

Inter-thread communication or Co-operation is all about allowing synchronized threads to


communicate with each other.
Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its
critical section and another thread is allowed to enter (or lock) in the same critical section to be
executed. It is implemented by following methods of Object class:
 wait()
 notify()
 notifyAll()

Methods for Inter Thread Communication:

Understanding the process of inter-thread communication:


The point to point explanation of the above diagram is as follows:
1. Threads enter to acquire lock.
2. Lock is acquired by on thread.
3. Now thread goes to waiting state if you call wait() method on the object. Otherwise it
releases the lock and exits.
4. If you call notify() or notifyAll() method, thread moves to the notified state (runnable state).
5. Now thread is available to acquire lock.
6. After completion of the task, thread releases the lock and exits the monitor state of the
object.
Difference between wait() and sleep() methods:

wait() sleep()

wait() method releases the lock sleep() method doesn't release the lock.

is the method of Object class is the method of Thread class

is the non-static method is the static method

is the non-static method is the static method

should be notified by notify() or notifyAll() after the specified amount of time, sleep is
methods completed.

Program to demonstrate Inter thread communication:

class Account
{
int amount=10000;
synchronized void withdraw(int amt)
{
System.out.println("going to withdraw...");
if(amount<amt){
System.out.println("Less balance; waiting for deposit...");
try{
wait();
}catch(Exception e){}
}
amount -= amt;
System.out.println("withdraw completed...");
System.out.println("Fresh Amount : " + amount);
}

synchronized void deposit(int amt)


{
System.out.println("going to deposit...");
amount+=amt;
System.out.println("deposit completed... ");
System.out.println("Fresh Amount : " + amount);
notify();
}
}

class ITCDemo
{
public static void main(String args[])
{
final Account c=new Account();
System.out.println("Initial Amount : " + c.amount);
new Thread(){
public void run(){c.withdraw(15000);}
}.start();
new Thread(){
public void run(){c.deposit(10000);}
}.start();
}
}

Output:
Files and IO Streams
I/O Streams:
• A Stream is linked to a physical layer by java I/O system to make input and output operation in
java.
• In general, a stream means continuous flow of data.
• Streams are clean way to deal with input/output without having every part of your code
understand the physical.
• An I/O Stream represents an input source or an output destination.
• A stream can represent many different kinds of sources and destinations, including disk files,
devices, other programs, and memory arrays.
• Streams support many different kinds of data, including simple bytes, primitive data types,
localized characters, and objects.
• Some streams simply pass on data; others manipulate and transform the data in useful ways.

No matter how they work internally, all streams present the same simple model to programs
that use them: A stream is a sequence of data. A program uses an input stream to read data from a
source, one item at a time:

Reading information into a program.

A program uses an output stream to write data to a destination, one item at time:

Writing information from a program.

Java encapsulates Stream under java.io package. Java defines two types of streams. They are,
1. Byte Stream : It provides a convenient means for handling input and output of byte.
2. Character Stream : It provides a convenient means for handling input and output of characters.
Character stream uses Unicode and therefore can be internationalized.
3. Byte stream is defined by using two abstract class at the top of hierarchy, they are InputStream and
OutputStream.
These two abstract classes have several concrete classes that handle various devices such as disk
files, network connection etc.
Some important Byte stream classes.
Stream class Description
BufferedInputStream Used for Buffered Input Stream.
BufferedOutputStream Used for Buffered Output Stream.
DataInputStream Contains method for reading java standard datatype
DataOutputStream An output stream that contain method for writing java standard data type
FileInputStream Input stream that reads from a file
FileOutputStream Output stream that write to a file.
InputStream Abstract class that describe stream input.
OutputStream Abstract class that describe stream output.
PrintStream Output Stream that contain print() and println() method

These classes define several key methods. Two most important are
1. read() : reads byte of data.
2. write() : Writes byte of data.

Character Stream Classes


Character stream is also defined by using two abstract class at the top of hierarchy, they are Reader and
Writer.

These two abstract classes have several concrete classes that handle unicode character.
Some important Charcter stream classes.
Stream class Description
BufferedReader Handles buffered input stream.
BufferedWriter Handles buffered output stream.
FileReader Input stream that reads from file.
FileWriter Output stream that writes to file.
InputStreamReader Input stream that translate byte to character
OutputStreamWrite
Output stream that translate character to byte.
r
PrintWriter Output Stream that contain print() and println() method.
Reader Abstract class that define character stream input
Writer Abstract class that define character stream output
Reading Console Input

We use the object of BufferedReader class to take inputs from the keyboard.

Reading input from keyboard in command window:

Output:

Explanation:
a) System.in object is predefined object of InputStream class, and it can read the keyboard input as
binary data.
b) InputStreamReader object converts the binary data into character data.
c) Buffered Reader object stores character data in a buffered memory, and converts into a string object.
d) Interger.parseInt() methods converts the string data into numerical data.

java.io.File class:
• The java.io.File class object refers to a reference object to an actual physical file in the memory.
( like a FILE pointer in C language)
• This class is used to know the properties of a file like
– path of the file,
– whether the file exists or not,
– whether the file is a file or a directory,
– length of the file, etc.
java.io.File class methods:
 File Constructors
o public File(String path)
o public File(String path, String name)
o public File(File dir, String name)
 File Methods
o public String getName()
o public String getPath()
o public String getAbsolutePath()
o public String getParent()
o public boolean exists() throws SecurityException
o public boolean canWrite() throws SecurityException
o public boolean canRead() throws SecurityException
o public boolean isFile() throws SecurityException
o public boolean isDirectory() throws SecurityException
o public boolean isAbsolute()
o public long lastModified() throws SecurityException
o public long length() throws SecurityException
o public boolean mkdir()
o public boolean mkdirs() throws SecurityException
o public boolean renameTo(File destination) throws SecurityException
o public boolean delete() throws SecurityException

Output:
Reading Content from the file:

import java.io.*;
class ReadFileDemo{
public static void main(String args[]) throws Exception
{
File f1=new File(args[0]);
byte[] b={};
if(f1.exists()){
FileInputStream fis=new FileInputStream(f1);
b=new byte[fis.available()];
fis.read(b);
String s=new String(b);
System.out.println("Contents of "+args[0]+ ": \n"+ s);
fis.close();
}else{
System.out.println("File does not exist");
}
}
}

Output:

Writing Content to the file:

import java.io.*;
class WriteFileDemo{
public static void main(String args[]) throws Exception
{
FileOutputStream fos=new FileOutputStream(args[0]);
System.out.println("File Opened, now writing contents");
String str = "Welcome to java programming." +
"\nThis is example for File operations.";
byte[] b = str.getBytes();
fos.write(b);
fos.flush();
System.out.println("contents written");
System.out.println("Closing File");
fos.close();
}
}

Output:

Copy the content of one file into another file:

import java.io.*;
public class CopyFile{
public static void main(String[] args) throws Exception
{
int i;
FileInputStream fin = new FileInputStream(args[0]);
FileOutputStream fout = new FileOutputStream(args[1]);
do {
i = fin.read();
if(i != -1) fout.write(i);
}while (i != -1);
fin.close();
fout.close();
}
}

Output:
RandomAccessFile
• The RandomAccessFile class provides a way to read from and write to a file in a nonsequential
manner.
• The constructor of RandomAccessFile class has two arguments. The first argument specifies the
file to open, either as a String or a File object.
• The second argument is a String that must be either "r" or "rw". If the second argument is "r",
the file is opened for reading only. If the argument is "rw", however, the file is opened for both
reading and writing.

RandomAccessFileDemo.java

import java.io.*;
class RandomAccessFileDemo {
public static void main(String args[]) throws Exception
{
RandomAccessFile raf = new RandomAccessFile(args[0], "rw");
raf.seek(10);
byte b1[] = new byte[15];
raf.read(b1);
System.out.println("Read from file : " + new String(b1));
raf.seek(raf.length());
String s1 = "This is end of the file.";
byte[] b2 = s1.getBytes();
raf.write(b2);
raf.close();
}
}
Output:

The content in the file before file operations: sample.txt


Welcome to java programming.
This is example for File operations.

The content in the file after file operations : sample.txt


Welcome to java programming.
This is example for File operations.This is end of the file.
List of questions asked in previous question papers:

PART-A

1) What is the purpose of isAlive () function in Java.


2) What is the importance of synchronization in java?
3) What is a daemon thread?
4) Differentiate between sleep () and wait ().
5) Discuss various methods used to create threads?
6) How to read data in java file? Discuss.
7) How does Java support inter thread communication?
8) List the thread states and give state transition diagram
9) Write about thread suspension and resume.
10) Define thread. How is it different from a process?
11) What is light weight process? Discuss.
12) Write a java program to create multiple threads.
13) List the methods in Thread class.
14) Give a note on volatile modifier.
15) Give a note on transient modifier.

PART-B

1. a) Write a Java program that prints numbers from 1 to 10 line by line after every 5 seconds
b) What is thread synchronization? Discuss with an example.
2. a) Write a Java program for creating four threads to perform the following operations
i) Getting N numbers as input ii) Printing the even numbers
iii) Printing the odd numbers iv) Computing the average
b) Explain how communication between threads takes place with a programming example.
3. a) Explain the following with necessary code snippets
i) Creating thread ii) Stopping and Blocking a Thread
b) "Threads can be given priorities" - Support this statement with suitable example.
4. a) Write a Java program to demonstrate multithreading operation.
b) Explain various thread states and properties in detail.
6. a) Explain life cycle of a thread with neat diagram
b) Discuss about thread synchronization.
7. a) Describe Java’s thread model.
b) What is a stream? What is the difference between byte streams and character streams? How are
they used to capture input from the user?
8. a) Write a program to implement multi thread programming.
b) Explain thread synchronization
9. a) Describe the need of thread synchronization. How is it achieved in Java programming? Explain with
a suitable program.
b) Differentiate between FileReader and BufferReader.
10. a) Explain thread life cycle and thread creation in Java.
b) Write a program to read user name from console and display some message for that user using
streams.
11. a) Explain about java.lang.thread package.
b) How to set priorities for threads? Discuss with examples.
12. a) What happen when PrintWriter method receives a string type argument?
b) Write a java program to display all odd numbered lines of a text file.
13. a) Discuss about reading console input.
b) Write a java program to implement producer consumer problem.
14. a) Write example that uses join ( ) to ensure that the main thread is the last to stop. Use isAlive () in
the same program.
b) Explain with example; explain how we set priority to threads.
15. a) Discuss about writing console output.
b) Write a java program to display all odd numbered lines of a text file.
16. a) Explain the Java thread model and its related issues in detail.
b) What is the need to assign priorities to threads? Explain with an example.

Assignment:

PART-A

1) Define thread. How is it different from a process?


2) What is the importance of synchronization in java?
3) What is a daemon thread?
4) Differentiate between sleep () and wait ().
5) How to read data in java file? Discuss.
6) List the methods in Thread class.
7) Give a note on volatile modifier.
8) Give a note on transient modifier.
9) What happen when PrintWriter method receives a string type argument?

PART-B

1. a) What is thread synchronization? Discuss with an example.


b) Explain how communication between threads takes place with a programming example.
2. a) Explain the following with necessary code snippets
i) Creating thread ii) Stopping and Blocking a Thread
b) "Threads can be given priorities" - Support this statement with suitable example.
3. a) Write a Java program to demonstrate multithreading operation.
b) Explain various thread states and properties in detail with a neat diagram
4. a) What is a stream? What is the difference between byte streams and character streams? How are
they used to capture input from the user?
b) Differentiate between FileReader and BufferReader.
5. a) Discuss about reading console input.
b) Discuss about writing console output.

PROGRAMS:

1. Write a Java program that prints numbers from 1 to 10 line by line after every 5 seconds
2. Write a Java program for creating four threads to perform the following operations
i) Getting N numbers as input ii) Printing the even numbers
iii) Printing the odd numbers iv) Computing the average
3. Write a Java program to demonstrate multithreading operation.
4. Write a java program to display all odd numbered lines of a text file.
5. Write a program to read user name from console and display some message for that user using
streams.
6. Write a java program to implement producer consumer problem.

You might also like