• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Threads in Java Tutorial for Beginners

April 17, 2012 by ProjectsGeek Leave a Comment

Threads in Java Tutorial for Beginners





Threads in Java Tutorial for Beginners is part of previous tutorial on Java core programming . As we know threads is the important feature of Java programming So , it’s necessary to learn concepts of threads in java .


Threads in Java Course Contents are Given below in Sequence

  • Introduction to Multitasking
  • Definition of Thread
  • Thread Methods
  • Synchronization
  • InterThread Communication
  • Questions

Starting with the Terms related to threads in Java …


What is Process ?


A process is a program in execution on system or computer . Two or more processes running concurrently in a computer is called multitasking. So , multiple process leads to multitasking .
Java also supports for multithreading. A Process can contain multiple threads to execute its different sections. So , Process having multiple threads is multithreading in Java .


These terms are important from many point of views , So I suggest you to keep these terms in mind .


What is Thread?


A thread is a line of execution of code . It is the smallest unit of code that is dispatched by the scheduler.


A process can contain multiple threads to execute its different sections. This is called multithreading.


Advantages of Threads –

  1. Can be created faster.
  2. Requires less overhead.
  3. Interprocess communication is faster.
  4. Context switching is faster.
  5. Maximum use of CPU time.

Some Common Thread Methods which are generally used for programming threads in Java codes are given below:


start();
Start the execution of thread.


stop();
Stop the execution of Thread.


suspend();
Suspend the execution of Thread.


resume();
Resume the execution of suspended Thread.
Common Thread Methods


sleep (long);
Suspend the execution of thread for a certain period of time . Time is in milliseconds.


How to Create a Thread?


A thread in Java is created as an Object of Class Thread.
Thread t = new Thread();


A thread in Java can also be created by implementing your class by Runnable Interface.


Example :


By default every Java class contains a main thread where the execution of the program starts.


class sample
{
public static void main (String args)
{
— Print even numbers from 0 to 100
— Print odd numbers from 0 to 100
}
}


Example : (Multithreading)


class sample
{
— New Thread Print even Numbers from 0 to 100.
public static void main (String args)
{
— Create a New Thread t1
— Starts a New Thread t1
— Print odd numbers from 0 to 100
}
}


Example : (Multithreading)


class sample extends Thread
{
— New Thread Print even Numbers from 0 to 100.
public static void main (String args)
{
sample s = new sample();
— Starts a New Thread t1
— Print odd numbers from 0 to 100
}
}


Example : (Multithreading)


class sample extends Thread
{
— New Thread Print even Numbers from 0 to 100.
public static void main (String args)
{
sample s = new sample();
s.start();
— Print odd numbers from 0 to 100
}
}


Example : (Multithreading)


class sample extends Thread
{
public void run() { — even Numbers from 0 to 100 }
public static void main (String args)
{
sample s = new sample();
s.start();
— Print odd numbers from 0 to 100
}
}


Thread Methods


User can create multiple threads in a Java program and set the priority of each.


int getPriority()
setPriority(int);


User can check whether the Thread is alive or dead.


boolean isAlive();


Waits forever for this Thread to die.


join()


Thread Methods


Returns a reference to the currently executing Thread object and is a static method.


currentThread();


Thread Names can be set by using :
String getName();
setName(String);


Example :
Class sample extends Thread
{
public static void main (String args[]) {
Thread t= Thread.currentThread();
System.out.println (t.getName());
System.out.println (t.getPriority());
t.setName (“myThread”);
t.setPriority(2);
System.out.println (t.getName());
System.out.println (t.getPriority());
} }


Synchronization


Two or more Thread accessing the same data simultaneously may lead to loss of data integrity.


For example, when two people access a saving account, it is possible that one person may overdraw and the cheque may bounce.


Monitor


Java uses the concept of monitor or a semaphore to enable this. A monitor is an object, used as a mutually exclusive lock.


At a time, only one thread can access the monitor. A second thread cannot enter the monitor until the first comes out. Till such time, the other thread is said to be waiting.
Monitor
The keyword synchronized is used in the code to enable synchronization.


The word synchronized can be used with a method or within a block.
public void synchronized request();


Inter Thread Communication


Java offers interprocess communication through the usage of wait(), notify() and notifyall() methods of Object class and all are synchronized methods.


Wait() – waits indefinitely on another thread of execution until it receives a notify() or notifyall() message.


Inter Thread Communication


notify() – This method wakes up a single thread waiting on the Object’s monitor.


notifyAll()- This methods wakes up all threads waiting on the Object’s monitor.




Questions ?




Define Thread. In Java what are different ways to create a Thread?


What are different Thread Methods present in class Thread?


Which is the default method executed by Created new Thread.?


What are the advantages of Threads over processes?

Other Projects to Try:

  1. First Program in Java
  2. Multiple Inheritance in java program
  3. File Handling and IO Handling in Java Programming
  4. Java Applet Tutorial for Beginners
  5. Java Tutorial for beginners – Introduction to Java

Filed Under: Java Tutorials

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy