You are on page 1of 2

2/14/14

Java threads sample code examples - Java Sample Programs

JAVA EXAMPLE PROGRAMS


0

Home Fundamentals Constructors Exception Handling Threads String Functions Generics Collections & Util Package Nested Classes Networking File I/O Operations Java Annotations JDBC Examples Java Interview Questions Java Interview Programs JUnit Search Algorithms Sorting Algorithms Data Structures JBoss Configurations

Java Threads Examples


Threading is a facility to allow multiple tasks to run concurrently within a single process. Threads are independent, concurrent execution through a program, and each thread has its own stack. In Java threads can be implemented in two ways. One is by 'Extending Thread Class' and the other way is by 'Implementing Runnable Interface' Extending Thread Class is required to 'override run()' method. The run method contains the actual logic to be executed by thread. Creation of thread object never starts execution, we need to call 'start()' method to run a thread. Examples gives you more details. Other methods supported by Threads are given below. join(): It makes to wait for this thread to die. You can wait for a thread to finish by calling its join() method. sleep(): It makes current executing thread to sleep for a specified interval of time. Time is in milli seconds. yield(): It makes current executing thread object to pause temporarily and gives control to other thread to execute. notify(): This method is inherited from Object class. This method wakes up a single thread that is waiting on this object's monitor to acquire lock. notifyAll(): This method is inherited from Object class. This method wakes up all threads that are waiting on this object's monitor to acquire lock. wait(): This method is inherited from Object class. This method makes current thread to wait until another thread invokes the notify() or the notifyAll() for this object.

Knowledge Center Difference between super() and thi

Java Threads Sample Code Examples


By Implementing Runnable Interface Example By Extending Thread Class Example Daemon Thread Example Thread Join Example Thread Sleep Example Thread Yield Example

super() is used to call super class c whereas this() used to call constru same class, means to call paramet constructors.

Find us on Facebook

Java Sample Programs


Like 1,608 people like Java Sample Programs

Facebook social plugin

Few Random Java Examples

What is generics wildcard argumen example. Write a program to find maximum words from a file.

Find out duplicate number between numbers.

Can you serialize static fields of a c

Placement Java Interview Question

How to convert byte array to inputs

How to get subset from sorted set?

www.java2novice.com/java_thread_examples/

How implement bounded types (imp

1/2

2/14/14

Java threads sample code examples - Java Sample Programs


How to zip a single file?

How implement bounded types (imp interface) with generics?

About Author
My name is Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment. If you come across any mistakes or bugs, please email me at java2novice@gmail.com or you can comment on the page.

Most Visited Pages


Freshers Java Interview Questions How to Create Java Custom Exception How to convert byte array to inputstream? Java StringTokenizer With Multiple De-limiters Sample Code Java Constructor Chaining Examples Write a program to find out duplicate characters in a string.

Other Interesting Sites


Techie Park Wikipedia query2nataraj.blogspot.in

Write a program to find maximum repeated words from a file. How to read file content using byte array?

Reference: Java Platform Standard Ed. 6 - API Specification | Java is registered trademark of Oracle. Privacy Policy | Copyright 2013 by Nataraja Gootooru. All Rights Reserved.

www.java2novice.com/java_thread_examples/

2/2

You might also like