Multithreading in Java
Multithreading in Java
class Ex1
{
public static void main(String arg[])
{
Thread obj=Thread.currentThread();
System.out.println(obj.getName());
System.out.println(obj.getPriority()); //0 to 9
System.out.println(obj.isAlive());
obj.setName("Jithesh");
obj.setPriority(9);
System.out.println(obj);
}
}
ex:2
class Ex2
{
public static void main(String arg[])
{
for(int i=0;i<10;i++)
{
System.out.println(i);
}
}
}
ex:3
class Ex2
{
public static void main(String arg[])
{
Thread obj=Thread.currentThread();
for(int i=0;i<10;i++)
{
try
{
obj.sleep(1000); // int msec
}catch(IntteruptedException e){}
System.out.println(i);
}
}
}
OR
class Ex2
{
public static void main(String arg[])throws Exception
{
Thread obj=Thread.currentThread();
for(int i=0;i<10;i++)
{
obj.sleep(1000); // int msec
System.out.println(i);
}
}
}
output
0
1
2
3
4
5
6
7
8
9
multithreading
*************
* extends Thread class
* implements Runnable Interface
class ex4
{
public static void main(String arg[])
{
A obj=new A();
B obj=new B();
}
}
output
A class 0
B class 0
....
...
class ex5
{
public static void main(String arg[])
{
A obj=new A();
B obj=new B();
}
}
ex 6
//setName(str);
start();
}
public void run()
{
for(int i=0;i<10;i++)
{
try
{
sleep(1000); // int msec
}catch(IntteruptedException e){}
System.out.println(getName()+i);
}
}
}
class ex5
{
public static void main(String arg[])
{
A obj=new A("Ilayaraja");
A obj1=new A("Vasudevan");
}
}