You are on page 1of 2

2/14/14

Thread by implementing Runnable interface 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 Thread By Implementing Runnable Interface


A Thread can be created by extending Thread class also. But Java allows only one class to extend, it wont allow multiple inheritance. So it is always better to create a thread by implementing Runnable interface. Java allows you to impliment multiple interfaces at a time. By implementing Runnable interface, you need to provide implementation for run() method. To run this implementation class, create a Thread object, pass Runnable implementation class object to its constructor. Call start() method on thread class to start executing run() method. Implementing Runnable interface does not create a Thread object, it only defines an entry point for threads in your object. It allows you to pass the object to the Thread(Runnable implementation) constructor.

Thread Sample Code


Code: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 p a c k a g ec o m . m y j a v a . t h r e a d s ; c l a s sM y R u n n a b l e T h r e a di m p l e m e n t sR u n n a b l e { p u b l i cs t a t i ci n tm y C o u n t=0 ; p u b l i cM y R u n n a b l e T h r e a d ( ) { } p u b l i cv o i dr u n ( ){ w h i l e ( M y R u n n a b l e T h r e a d . m y C o u n t< =1 0 ) { t r y { S y s t e m . o u t . p r i n t l n ( " E x p lT h r e a d :" + ( + + M y R u n n a b l e T h r e a d . m y C o u n t ) ) ; T h r e a d . s l e e p ( 1 0 0 ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ni e x ){ S y s t e m . o u t . p r i n t l n ( " E x c e p t i o ni nt h r e a d :" + i e x . g e t M e s s a g e ( ) ) ; } } }
?

Knowledge Center Java class can be private?

We can not declare top level class Java allows only public and default top level classes in java. Inner clas private.

} p u b l i cc l a s sR u n M y T h r e a d{ p u b l i cs t a t i cv o i dm a i n ( S t r i n ga [ ] ) { S y s t e m . o u t . p r i n t l n ( " S t a r t i n gM a i nT h r e a d . . . " ) ; M y R u n n a b l e T h r e a dm r t=n e wM y R u n n a b l e T h r e a d ( ) ; T h r e a dt=n e wT h r e a d ( m r t ) ; t . s t a r t ( ) ; w h i l e ( M y R u n n a b l e T h r e a d . m y C o u n t< =1 0 ) { t r y { S y s t e m . o u t . p r i n t l n ( " M a i nT h r e a d :" + ( + + M y R u n n a b l e T h r e a d . m y C o u n t ) ) ; T h r e a d . s l e e p ( 1 0 0 ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ni e x ) { S y s t e m . o u t . p r i n t l n ( " E x c e p t i o ni nm a i nt h r e a d :" + i e x . g e t M e s s a g e ( ) ) ; } } S y s t e m . o u t . p r i n t l n ( " E n do fM a i nT h r e a d . . . " ) ; } }

Find us on Facebook

Java Sample Programs


Like

Example Output
Starting Main Thread... Main Thread: 1 Expl Thread: 2 Main Thread: 3 Expl Thread: 4 Main Thread: 5 Expl Thread: 6 Main Thread: 7 Expl Thread: 8 Main Thread: 9 Expl Thread: 10 Main Thread: 11 End of Main Thread...
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.

Other Thread Examples


By Extending Thread Class Example Daemon Thread Example Thread Join Example Thread Sleep Example Thread Yield Example

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/implementing_runnable/

How implement bounded types (imp

1/2

2/14/14

Thread by implementing Runnable interface 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/implementing_runnable/

2/2

You might also like