PIMPRI CHINCHWAD POLYTECHNIC
PIMPRI CHINCHWAD EDUCATION TRUST’S
PIMPRI CHINCHWAD POLYTECHNIC
I.S.O 9001:2008 Certified. Approved by A.I.C.T.E. and Affiliated to M.S.B.T.E. Mumbai
PCET Trust
SPOTLIGHT
Pimpri chinchwad polytechnic is best diploma college for computer science.

Computer Blog


Threads in JAVA


What are Java Threads?

  • Facility to allow multiple activities within a single process
  • Referred as lightweight process
  • A thread is a series of executed statements
  • Each thread has its own program counter, stack and local variables
  • A thread is a nested sequence of method calls
  • Its shares memory, files and per-process state

team

In Pimpri Chinchwad Polytechnic in Computer Science Department gives full practical as well as theoretical knowledge about Threads in Java. Students develop their own programming knowledge about Java coding during practical sessions.

What’s the need of a thread or why we use Threads?

  • To perform asynchronous or background processing
  • Increases the responsiveness of GUI applications
  • Take advantage of multiprocessor systems
  • Simplify program logic when there are multiple independent entities

What happens when a thread is invoked?

When a thread is invoked, there will be two paths of execution. One path will execute the thread and the other path will follow the statement after the thread invocation. There will be a separate stack and memory space for each thread.


Risk Factor

  • Proper co-ordination is required between threads accessing common variables [use of synchronized and volatile] for consistence view of data
  • overuse of java threads can be hazardous to program’s performance and its maintainability.

Threads in Java

Java threads facility and API is deceptively simple:

Every java program creates at least one thread [ main() thread ]. Additional threads are created through the Thread constructor or by instantiating classes that extend the Thread class.


Thread creation in Java

Thread implementation in java can be achieved in two ways:

  • Extending the java.lang.Thread class
  • Implementing the java.lang.Runnable Interface
  • .

By extending thread class

  • The class should extend Java Thread class.
  • The class should override the run() method.
  • The functionality that is expected by the Thread to be executed is written in the run() method.

void start(): Creates a new thread and makes it runnable.

void run(): The new thread begins its life inside this method.


team

Example.

public class MyThread extends Thread {
public void run(){
System.out.println("thread is running...");
}
public static void main(String[] args) {
MyThread obj = new MyThread();
obj.start();
}

2) By Implementing Runnable interface

  • The class should implement the Runnable interface
  • The class should implement the run() method in the Runnable interface
  • The functionality that is expected by the Thread to be executed is put in the run() method

Example:

public class MyThread implements Runnable {
public void run(){
System.out.println("thread is running..");
}
public static void main(String[] args) {
Thread t = new Thread(new MyThread());
t.start();
}experts there.

Thread life cycle in java


team

  • The start method creates the system resources, necessary to run the thread, schedules the thread to run, and calls the thread’s run method.
  • A thread becomes “Not Runnable” when one of these events occurs:
    • If sleep method is invoked.
    • The thread calls the wait method.
    • The thread is blocking on I/O.
  • A thread dies naturally when the run method exits.

Pimpri Chinchwad Polytechnic is the best computer diploma engg college in Pune which provides vast knowledge about programming, where many sessions are conducted for students to gain knowledge about coding.

Blocking Threads

  • When reading from a stream, if input is not available, the thread will block
  • Thread is suspended (“blocked”) until I/O is available
  • Allows other threads to automatically activate
  • When I/O available, thread wakes back up again
    • Becomes “runnable” i.e. gets into ready state.

Grouping of threads

  • Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually.
  • To put a new thread in a thread group the group must be explicitly specified when the thread is created
  • .
    • public Thread(ThreadGroup group, Runnable runnable)
    • public Thread(ThreadGroup group, String name)
    • public Thread(ThreadGroup group, Runnable runnable, String name)

Target keywords: thread life cycle in java, java threading tutorial, using threads in java, javathread run.

These sessions are conducted under Java Programming Lecture in Pimpri Chinchwad Polytechnic which is one of the best college in Pune




Pallavi Maladkar

(Computer Department)

Pimpri Chinchwad Polytechnic

Contact

Sector -26, Pradhikaran, Nigdi,
Near Akurdi Railway Station,
Pune - 411 044.
Contact No. - 020 - 27654156,
                          020 - 27658797.
Email: principal@pcpolytechnic.com

© Copyright 2014 PCP