Sei sulla pagina 1di 2

2/14/14

Thread Join 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 Join Examples


Imagine the following scenario. You are preparing for tomorrow's final examination and feel a little hungry. So, you give your younger brother ten bucks and ask him to buy a pizza for you. In this case, you are the main thread and your brother is a child thread. Once your order is given, both you and your brother are doing their job concurrently (i.e., studying and buying a pizza). Now, we have two cases to consider. First, your brother brings your pizza back and terminates while you are studying. In this case, you can stop studying and enjoy the pizza. Second, you finish your study early and sleep (i.e., your assigned job for today - study for tomorrow's final exam - is done) before the pizza is available. Of course, you cannot sleep; otherwise, you won't have a chance to eat the pizza. What you are going to do is to wait until your brother brings the pizza back. A thread can execute a thread join to wait until the other thread terminates A parent thread may join with many child threads created by the parent. Or, a parent only join with some of its child threads, and ignore other child threads. In this case, those child threads that are ignored by the parent will be terminated when the parent terminates.

Thread Join 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 ; i m p o r tj a v a . u t i l . A r r a y L i s t ; i m p o r tj a v a . u t i l . L i s t ; p u b l i cc l a s sM y T h r e a d J o i n{ p u b l i cs t a t i cL i s t < S t r i n g >n a m e s=n e wA r r a y L i s t < S t r i n g > ( ) ; p u b l i cs t a t i cv o i dm a i n ( S t r i n ga [ ] ) { L i s t < S a m p l e T h r e a d >l i s t=n e wA r r a y L i s t < S a m p l e T h r e a d > ( ) ; f o r ( i n ti = 0 ; i < 5 ; i + + ) { S a m p l e T h r e a ds=n e wS a m p l e T h r e a d ( ) ; l i s t . a d d ( s ) ; s . s t a r t ( ) ; } f o r ( S a m p l e T h r e a ds t : l i s t ) { t r y { s t . j o i n ( ) ; }c a t c h( E x c e p t i o ne x ) { } } S y s t e m . o u t . p r i n t l n ( n a m e s ) ; }
?

Knowledge Center doPost Vs doGet methods

doGet() method is used to get info while doPost() method is used for information. doGet() requests can amount of information and is limit 255 characters. However, doPost() passes all of its data, of unlimited doGet() request is appended to the in a query string and this allows th is visible to the client, whereas a d request passes directly over the so connection as part of its HTTP req and the exchange are invisible to t

c l a s sS a m p l e T h r e a de x t e n d sT h r e a d { p u b l i cv o i dr u n ( ) { f o r ( i n ti = 0 ;i < 1 0 ;i + + ) { t r y { T h r e a d . s l e e p ( 1 0 ) ; }c a t c h ( E x c e p t i o ne x ) { } } M y T h r e a d J o i n . n a m e s . a d d ( g e t N a m e ( ) ) ; }

Example Output
[Thread-0, Thread-2, Thread-1, Thread-4, Thread-3]

Other Thread Examples


By Implementing Runnable Interface Example By Extending Thread Class Example Daemon Thread 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/thread_join/

1/2

2/14/14

Thread Join 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/thread_join/

2/2

Potrebbero piacerti anche