Tutorial Addendum On Java - Accoutrement

travelappsios lifestyleappsios utilitiesappsios
 31 December 18:00   

    



    



    

Output:

    

 

    

Hello world! - From the capital program .

    

Hello world! - From a thread.

    



    



    

Note that:

    



        

  • The program behaves the aforementioned way as the antecedent program : HelloThread.

        



  •     

  • The Cilia item t is created with the appropriate Cilia constructor,

        

    which takes a Runnable item as input. If you alpha a Cilia item created

        

    in this way, the run() adjustment of the Runnable item will be accomplished as

        

    a new thread.

        



  •     

  • Since our chic is not extending the Cilia chic any more, we charge to

        

    call the sleep() absolutely by prefixing the chic name: Thread.

        



  •     



    



    

May be you are apprehensive why we charge the additional adjustment of creating a new thread,

    

which seems to be beneath beeline advanced than the first method? The acknowledgment is that

    

Java classes can not be continued from two altered abject classes. So if you are

    

in a bearings area you wish to make a new chic by extending an absolute

    

class to accede some nice appearance of that class, and you aswell wish to make

    

the new chic executable as a thread, you accept to use the additional adjustment to implement

    

the Runnable interface in your new class.

    



    

CPU Time Sharing

    



    

Question: If there are 5 appliance program s active on a individual CPU system,

    

and there 5 accoutrement active in anniversary of the 5 running

    

application program s, how all the 25 accoutrement get accomplished accompanying on that

    

one CPU?

    



    

The acknowledgment is that those 25 accoutrement do not get accomplished accompanying at any

    

given instance of time. They get accomplished one at a time in turns to allotment

    

the processing time of the individual CPU. The afterward simple archetype illustrates

    

how the processing time can be aggregate by assorted threads.

    



    

Assuming that:

    



        

  • The operating arrangement is advised to assassinate the active appliance program one

        

    at a time in turns. For anniversary turn, the called appliance get accomplished for

        

    only 1 millisecond.

        



  •     

  • If an appliance program gets executed, it will assassinate the active accoutrement one

        

    at a time in turns. For anniversary turn, the called cilia gets accomplished for only

        

    0.01 millisecond.

        



  •     

  • There are 5 accoutrement active in anniversary of the 5 active appliance program s.

        



  •     

  • The times absorb on about-face accoutrement and appliance program in the CPU are

        

    ignorable.

        



  •     



    



    

If we let the arrangement runs for 0.1 second, anniversary appliance program will be executed

    

20 times for a absolute time of 20 milliseconds, and anniversary cilia will be accomplished

    

400 times for a absolute time of 4 milliseconds. So, for a atom of a additional

    

(1/10 second), all accoutrement in all applications program s will be accomplished for about

    

4 milliseconds. This will create you feel like all accoutrement get accomplished simultaneously.

    



    

Now let s use the afterward program to see how anniversary cilia is accepting its share

    

of the CPU time:

    

 

    

/**

    

* RacingThread.java

    

* Absorb (c) 2002 by Dr. Yang

    

*/

    

import java.util.*;

    

class RacingThread extends Cilia {

    

clandestine changeless final continued s_maxi = 1000000; // best amount of steps

    

clandestine changeless final int t_maxi = 5; // best amount of threads

    

clandestine changeless long[] t_done = new long[t_maxi];

    

clandestine changeless int t_last = -1; // basis of the endure thread

    

clandestine int t_indx;

    

clandestine changeless int n_prime = 0;

    

accessible changeless abandoned main(String[] a) {

    

System.out.println("Priority: (min, norm, max) = ("+

    

Thread.MIN_PRIORITY+", "+

    

Thread.NORM_PRIORITY+", "+

    

Thread.MAX_PRIORITY+")");

    

continued start_time = new Date().getTime();

    

for (int i=0; i<t_maxi; i++) {

    

RacingThread t = new RacingThread();

    

if (i==0) t.setPriority(Thread.MIN_PRIORITY);

    

abroad if (i==1) t.setPriority(Thread.NORM_PRIORITY);

    

abroad t.setPriority(Thread.MAX_PRIORITY);

    

t.start();

    

}

    

System.out.print("Threads: ");

    

for (int i=0; i<t_maxi; i++) {

    

System.out.print(i+" ");

    

}

    

System.out.print("Time");

    

while(true) {

    

try {

    

sleep(100);

    

} bolt (InterruptedException e) {

    

System.out.println("Interrupted.");

    

}

    

System.out.print("
Steps: ");

    

for (int i=0; i<t_maxi; i++) {

    

System.out.print(t_done[i]+" ");

    

}

    

System.out.print((new Date()).getTime()-start_time);

    

}

    

}

    

accessible RacingThread() {

    

t_last++;

    

t_indx = t_last;

    

t_done[t_indx] = 0;

    

}

    

accessible abandoned run() {

    

for (long s=0; s<s_maxi; s++) {

    

int n = 0;

    

for (int i=3; i<100; i++) {// accumulate it active for some time

    

boolean is_prime = true;

    

for (int j=2; j<i; j++) {

    

is_prime = (i%j>0);

    

if (!is_prime) break;

    

}

    

if (is_prime) n++;

    

}

    

n_prime = n;

    

t_done[t_indx] = s;

    

}

    

}

    

}

    



    



    

 


Tags: program, programs, times, system, application, class, final, start, method, prime, public, second, turns, object, print

 thread, threads, system, executed, class, program, priority, application, private, prime, print, static, running, racingthread, second, start, object, programs, method, setpriority, milliseconds, runnable, public, turns, times, , system out, private static, application program, setpriority thread, new class, new date gettime, thread norm priority, private static final,

Share Tutorial Addendum On Java - Accoutrement:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  java