Vous êtes sur la page 1sur 2

2/14/14

Thread by extending thread class 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 Extending Thread Class


A thread can be created in java by extending Thread class, where you must override run() method. Call start() method to start executing the thread object.

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 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 S m p T h r e a de x t e n d sT h r e a d { 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 cv o i dr u n ( ) { w h i l e ( M y S m p 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 S m p 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 ( ) ) ; } } } } p u b l i cc l a s sR u n 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 S m p T h r e a dm s t=n e wM y S m p T h r e a d ( ) ; m s t . s t a r t ( ) ; w h i l e ( M y S m p 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 S m p 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 . . . " ) ; } }
?

Knowledge Center

When to use LinkedList or ArrayLis

Example Output
Starting Main Thread... Main Thread: 1 Expl Thread: 2 Expl Thread: 3 Main Thread: 4 Expl Thread: 5 Main Thread: 5 Expl Thread: 6 Main Thread: 7 Main Thread: 8 Expl Thread: 9 Expl Thread: 11 Main Thread: 10 End of Main Thread...

Accessing elements are faster with because it is index based. But acce difficult with LinkedList. It is slow is to access any element, you need through the elements one by one. and deletion is much faster with Li because if you know the node, just pointers before or after nodes. Ins deletion is slow with ArrayList, thi during these operations ArrayList n adjust the indexes according to de insetion if you are performing on m indexes. Means, an ArrayList havin elements, if you are inserting at in you need to shift the indexes abov more.

Other Thread Examples


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

Find us on Facebook

Java Sample Programs


Like 1,608 people like Java Sample Programs

Facebook social plugin

www.java2novice.com/java_thread_examples/extending_thread_class/

1/2

2/14/14

Thread by extending thread class sample code examples - Java Sample Programs
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?

How implement bounded types (imp interface) with generics? How to zip a single file?

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. Write a program to find maximum repeated words from a file. How to read file content using byte array?

Other Interesting Sites


Techie Park Wikipedia query2nataraj.blogspot.in

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

2/2

Vous aimerez peut-être aussi