Beruflich Dokumente
Kultur Dokumente
{
Threadt;
newThreadClass()
{
t=newThread(this,"ThreadHijo");
System.out.println("Threadcreado:"+t);
t.start();
}
publicvoidrun()
{
try
{
for(inti=1;i<=5;i++)
{
System.out.println(t+"ciclo:"+i);
Thread.sleep(100);
}
}
catch(InterruptedExceptionobj)
{
System.out.println("Thread:"+t+"interrumpido");
}
}
}
classisAliveDemo
{
publicstaticvoidmain(Stringargs[])
newThreadClassobj=newnewThreadClass();
System.out.println(obj.t+"estavivo?:"+obj.t.isAlive());
try
for(inti=1;i<=5;i++)
{
System.out.println("MainThreadciclo:"+i);
Thread.sleep(200);
}
catch(InterruptedExceptione)
{
System.out.println("Mainthreadesinterrumpido");}
System.out.println(obj.t+"estavivo?:"+obj.t.isAlive());
System.out.println("MainThreadseestasaliendo");
}
}
classChildThreadimplementsRunnable
{
Threadt;
ChildThread()
{
t=newThread(this,"ThreadHijo");
System.out.println("Threadcreado:"+t);
t.start();
}
publicvoidrun()
{
try
{
for(inti=1;i<=5;i++)
{
System.out.println(t+"ciclo:"+i);
Thread.sleep(500);
}
}
catch(InterruptedExceptionobj)
{
System.out.println("Thread:"+t+"interrumpido");
}
}
}
classjoinDemo
{
publicstaticvoidmain(Stringargs[])
{
ChildThreadobj=newChildThread();
System.out.println(obj.t+"estavivo?:"+obj.t.isAlive());
try
{
System.out.println("Mainthreadesperandoqueelthreadhijotermine");
obj.t.join();
}
catch(InterruptedExceptione)
{
System.out.println("Mainthreadesinterrumpido");}
System.out.println(obj.t+"estavivo?:"+obj.t.isAlive());
System.out.println("MainThreadseestasaliendo");
}
}
classnewThreadClassimplementsRunnable
{
StringThreadName;
newThreadClass(Stringname)
{
ThreadName=name;
Threadt=newThread(this,ThreadName);
System.out.println("Threadcreated:"+t);
t.start();
publicvoidrun()
{
try
{
for(inti=1;i<=5;i++)
{
System.out.println(ThreadName+"loop:"+i);
Thread.sleep(500);
}
}
catch(InterruptedExceptionobj)
{
System.out.println("Thread:"+ThreadName+"interrupted");
}
System.out.println(ThreadName+"isexiting");
}
}
classMultipleThread
{
publicstaticvoidmain(Stringargs[])
{
newnewThreadClass("FirstChildThread");//CreatingfirstThread
newnewThreadClass("SecondChildThread");//CreatingsecondThread
try
{
for(inti=1;i<=5;i++)
{
System.out.println("MainThreadloop:"+i);
Thread.sleep(1000);
}
}
catch(InterruptedExceptionobj)
{
System.out.println("Mainthreadisinterrupted");
}
System.out.println("MainThreadisterminatingnow");
}
}
classThread1
{
voidcall()
System.out.println("primerasentencia");
try
{
Thread.sleep(1000);
}
catch(Exceptione)
{
System.out.println("Error"+e);
System.out.println("segundasentencia");
}
}
classThread2extendsThread
{
Thread1t;
publicThread2(Thread1t)
{
this.t=t;
publicvoidrun()
{
t.call();
}
}
publicclassNotSynchronized
{
publicstaticvoidmain(Stringargs[])
{
Thread1obj1=newThread1();
Thread2Obja=newThread2(obj1);
Thread2Objb=newThread2(obj1);
Obja.start();
Objb.start();
}
}
classChildThreadimplementsRunnable
{
Threadt;
ChildThread(intp)
{
t=newThread(this,"ThreadHijo");
t.setPriority(p);
System.out.println("Threadcreado:"+t);
}
publicvoidrun()
{
try
{
for(inti=1;i<=5;i++)
{
System.out.println(t+"ciclo:"+i);
Thread.sleep(500);
}
}
catch(InterruptedExceptionobj)
{
System.out.println("Thread:"+t+"interrumpido");}
}
}
classPriorityDemo
{
publicstaticvoidmain(Stringargs[])
{
ChildThreadobj1=newChildThread(Thread.NORM_PRIORITY2);
ChildThreadobj2=newChildThread(Thread.NORM_PRIORITY+2);
ChildThreadobj3=newChildThread(Thread.NORM_PRIORITY+3);
obj1.t.start();
obj2.t.start();
obj3.t.start();
try
{
System.out.println("Mainthreadesperandoqueelthreadhijotermine");
obj1.t.join();
obj2.t.join();
obj3.t.join();
}
catch(InterruptedExceptione)
{
System.out.println("Mainthreadesinterrumpido");}
System.out.println(obj1.t+"estavivo?:"+obj1.t.isAlive());
System.out.println(obj2.t+"estavivo?:"+obj2.t.isAlive());
System.out.println(obj3.t+"estavivo?:"+obj3.t.isAlive());
System.out.println("MainThreadseestasaliendo");
}
}
classNewThreadimplementsRunnable
{
Threadt;
NewThread()
{
t=newThread(this,"ThreadHijo");
System.out.println("ThreadHijo:"+t);
t.start();
}
publicvoidrun()
{
//implementandoelmetodorun()delainterfazRunnable
System.out.println("ElThreadHijoacomenzado");
System.out.println("Saliendodelthreadhijo");
}
}
classThreadClass
{
publicstaticvoidmain(Stringargs[])
{
newNewThread();
System.out.println("ElMainthreadcomienza");
try
{
Thread.sleep(5000);
catch(InterruptedExceptione)
{
System.out.println("Elmainthreadesinterrumpido");}
System.out.println("Saliendodelmainthread");
}
}
classThreadDemoextendsThread
{
ThreadDemo()
{
super("ThreadHijo");//llamaalconstructordelasuperclase
System.out.println("ThreadHijo:"+this);
start();
}
publicvoidrun()
{
System.out.println("Elthreadhijoacomenzado");
System.out.println("Saliendodelthreadhijo");
}
}
classThreadDemoClass
{
publicstaticvoidmain(Stringargs[])
{
newThreadDemo();
System.out.println("Elmainthreadacomenzado");
System.out.println("Elmainthreadseestadurmiendo");
try
Thread.sleep(1000);
}
}
}
catch(InterruptedExceptione)
{
System.out.println("Elmainthreadinterrumpido");
}
System.out.println("Saliendodelmainthread");