Download Multi Threading Examples docx

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
package multithread;
public class multithread {
public static void main(String[] args) {
// TODO Auto-generated method stub
FiveTable f=new FiveTable();
SevenTable s=new SevenTable();
ThirteenTable t=new ThirteenTable();
f.start();
s.start();
try {
s.sleep(1000);
} catch (Exception e) {System.out.println ("Exception thread sleep");}
t.start();
System.out.println("End of Main Thread") ; } }
class FiveTable extends Thread {
public void run() {
try {
Thread.sleep(1000);
} catch (Exception e) {System.out.println ("Exception thread
sleep");}
for (int i=1; i<=5; i++ )
System.out.println
(i + " * 5 = " + (i*5));
System.out.println("End of 5 Thread") ; } }
class SevenTable extends Thread {
public void run() {
for (int i=1; i<=5; i++ )
System.out.println -----Output-----this
change for different runs
based on thread priority scheduling by O/S / JVM ---------End
1 *
2 *
3 *
4 *
5 *
End
End
1 *
2 *
3 *
4 *
5 *
End
of Main Thread
5 = 5
5 = 10
5 = 15
5 = 20
5 = 25
of 5 Thread
1 * 7 = 7
2 * 7 = 14
3 * 7 = 21
4 * 7 = 28
5 * 7 = 35
of 7 Thread
13 = 13
13 = 26
13 = 39
13 = 52
13 = 65
of 13 Thread
order can
-------------------------------------------- implements Runnable example--------public class ThreadRunnableEx implements Runnable {
public void run(){
for (int i=1; i<=6; i++)
System.out.println("thread is running..");
}
public static void main(String[] args) {
Thread t = new Thread(new ThreadRunnableEx());
t.start();
System.out.println("
End of Main Thread ");
} }
-----output --------------This order can vary for different runs-----End of Main Thread
thread is running..
thread is running..
thread is running..
thread is running..
thread is running..
thread is running..
-------------------------------------Another implements Runnable Example----------------public class RunnableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t = new Thread(new X());
t.start();
System.out.println("
End of Main Thread " );
}
}
class X implements Runnable {
public void run(){
for (int i=1; i<= 10; i++){
System.out.println("\t Thread X:" + i );
}
System.out.println("End of Thread X" );
}
}
-----------Typical output May change based on thread priority----End of Main Thread
Thread X:1
Thread X:2
Thread X:3
Thread X:4
Thread X:5
Thread X:6
Thread X:7
Thread X:8
Thread X:9
Thread X:10
End of Thread X
One more Example which throws my own exception
import java.lang.Exception;
public class TestMyException {
public static void main(String[] args) {
int x=5,y=1000;
float z=(float)x/(float)y;
System.out.println("z must be <0.01 to generate MyException");
System.out.println("z is=" +z);
try{
if(z<0.01){
throw new Exception("num is smaller than 0.01, It is= " +z);
}
}
catch(Exception e){
System.out.println("x");
System.out.println(e.getMessage());
System.out.println("You "+z + " are NOT welcome");
}
finally{
System.out.println("Your Program Worked Well " + "You are welcome");
}
} }
Related documents