Beruflich Dokumente
Kultur Dokumente
t1.start();
t2.start();
t3.start();
System.out.println("Multi-Threading is Over");
// getName() method
//Thread Priority
class A extend Thread
{
public void run()
{
System.out.println("Thread A started");
for(i=1;i<=4;i++)
{
System.out.println("From Thread A:i=" +i);
}
System.out.println("Exit from A");
}
}
class ThreadPriority
{
public static void main(String[] args)
{
A threadA = new A();
B threadB = new B();
C threadC = new C();
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority() + 1);
threadA.setPriority(Thread.MIN_PRIORITY);
// run3 method
publicclassRunExp3extendsThread
{
publicvoidrun()
{
for(inti=1;i<6;i++)
{
try
{
Thread.sleep(500);
}catch(InterruptedExceptione){System.out.println(e);}
System.out.println(i);
}
}
publicstaticvoidmain(Stringargs[])
{
RunExp3t1=newRunExp3();
RunExp3t2=newRunExp3();
t1.run();
t2.run();
}
}
//isAlive() method
//with join()
try {
c1.join(); // Waiting for c1 to finish
}
catch (InterruptedException ie) {
}
c2.start();
}
}
//Synchronized method
class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
//Maximum priority
//Minimum priority
//Normal priority
class Q {
int n;
boolean valueset = false;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
class Thread3b {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("press ctrl+c to exit");
}
}
//sleep() method
//synchronized method
class Table{
synchronized void printTable(int n){//method synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}