Tutorial Addendum On Java - Accoutrement

travelappsios lifestyleappsios utilitiesappsios
 31 December 18:00   

    



    



    

The afterward program shows that how assorted accoutrement can plan calm

    

to account how some weekdays in anniversary year for 2000 years starting

    

from year 2000. In this program , an array, y_days, is declared as static,

    

so it can be aggregate by all the accoutrement to abundance the affected result,

    

the amount of weekdays of a specific year.

    

 

    

/**

    

* WeekDayCounter.java

    

* Absorb (c) 2002 by Dr. Yang

    

*/

    

import java.util.*;

    

class WeekDayCounter extends Cilia {

    

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

    

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

    

clandestine int t_indx; // basis of this thread

    

clandestine changeless int t_year = new int;

    

clandestine changeless boolean t_done = new boolean;

    

clandestine changeless final int y_maxi = 2000; // best amount of years

    

clandestine changeless final int s_year = 2000; // starting year

    

clandestine changeless int y_days = new int; // weekdays

    

clandestine changeless int y_logs = new int; // tracking the thread

    

accessible changeless abandoned main(String a) {

    

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

    

y_days = 0;

    

y_logs = 0;

    

}

    

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

    

WeekDayCounter t = new WeekDayCounter();

    

t.start();

    

}

    

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

    

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

    

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

    

}

    

Date t1 = new Date();

    

while(true) {

    

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

    

boolean done = true;

    

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

    

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

    

done = done && t_done;

    

}

    

if (done) break;

    

try {

    

sleep(100);

    

} bolt (InterruptedException e) {

    

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

    

}

    

}

    

Date t2 = new Date();

    

System.out.println("
Time = "+(t2.getTime()-t1.getTime()));

    

System.out.println("Year, Threads");

    

for (int y=s_year; y<s_year+y_maxi; y++) {

    

if (y_logs>1) {

    

System.out.println(y+", "+y_logs);

    

}

    

}

    

}

    

accessible WeekDayCounter() {

    

t_last++;

    

t_indx = t_last;

    

t_done = false;

    

t_year = 0;

    

}

    

accessible abandoned run() {

    

GregorianCalendar c = new GregorianCalendar();

    

for (int y=s_year; y<s_year+y_maxi; y++) {

    

t_year = y;

    

if (y_days==0) {

    

y_days = -1;

    

c.clear();

    

c.set(y,0,1); // first day of the year

    

int n = 0;

    

while (c.get(Calendar.YEAR)<=y) {

    

if (c.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY) n++;

    

if (c.get(Calendar.DAY_OF_WEEK)==Calendar.TUESDAY) n++;

    

if (c.get(Calendar.DAY_OF_WEEK)==Calendar.WEDNESDAY) n++;

    

if (c.get(Calendar.DAY_OF_WEEK)==Calendar.THURSDAY) n++;

    

if (c.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY) n++;

    

c.add(Calendar.DATE,1);

    

}

    

y_days = n;

    

y_logs += 1;

    

}

    

}

    

t_done = true;

    

}

    

}

    



    



    

When accoutrement are launched, they will all alpha to plan on one year at

    

time, alpha with the aforementioned starting year, 2000. After any coordination,

    

they will all plan on year 2000, then 2001. This is not what we want.

    

We charge to acquaint anniversary cilia to skip the year if addition cilia has already

    

finished adding for that year. This is done by initialize y_days with 0

    

for all the years at the alpha of the program , and ambience y_days of

    

a year to the affected aftereffect of that year. If addition cilia is active

    

behind and alcove this that year later, it checks the amount in y_days for

    

that year. If the amount is not 0, it will skip that year.

    



    

However, just ambience y_days to the aftereffect of the end of the adding is

    

not enough. Because artful the amount of weekdays of one year will yield some time,

    

and during this period, addition cilia ability still aces up that year and

    

work on that year, because the y_days amount is still 0. A simple way to stop

    

this aberration is to set y_days to -1 at the alpha of the calculation.

    



    

The program is aswell architecture to verify if a program we can added plan done with

    

more threads. Actuality is the after-effects of the program with altered amount of threads:

    



    

Output of 1 thread:

    

 

    

Thread: 0

    

Year: 0

    

Year: 2001

    

Year: 2025

    

Year: 2044

    

Year: 2077

    

Year: 2112

    

......

    

Year: 3824

    

Year: 3859

    

Year: 3895

    

Year: 3930

    

Year: 3966

    

Year: 3999

    

Time = 6029

    

Year, Threads

    



    



    

 


Tags: years, program, system, starting, beginning, final, value, calculation, public, result, print

 calendar, thread, threads, static, private, system, program, weekdaycounter, println, weekdays, print, value, calculation, beginning, public, result, years, final, boolean, starting, , system out, private static, calendar day, week calendar, static int, year 2000, static final, final int, new int, private static final, maxi private static,

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