Thread and Multithreading in Java for beginners(Part 2)

For Part 1
http://thebadengineer.com/threads-and-multithreading-in-java-for-beginers-part-1/

We will learn how to setDaemon and interrupt the thread process..

Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection.

Here is the code to make the thread set as daemon:

public class ThreadLongTaskDaemon {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new LongComputationTask(new BigInteger("200000"), new BigInteger("100000000")));

        thread.setDaemon(true);
        thread.start();
        Thread.sleep(100);
    }

    private static class LongComputationTask implements Runnable {
        private BigInteger base;
        private BigInteger power;

        public LongComputationTask(BigInteger base, BigInteger power) {
            this.base = base;
            this.power = power;
        }

        @Override
        public void run() {
            System.out.println(base + "^" + power + " = " + pow(base, power));
        }

        private BigInteger pow(BigInteger base, BigInteger power) {
            BigInteger result = BigInteger.ONE;

            for (BigInteger i = BigInteger.ZERO; i.compareTo(power) != 0; i = i.add(BigInteger.ONE)) {
                result = result.multiply(base);
            }
            return result;
        }
    }
}

It makes the thread as daemon and then sleeps for 100ms and exit the process.
Advantage of daemon threads is even if the daeomn threads are running we can exit the process , this is not the same case as in user level thread as follows:

public class ThreadLongTask {

    public static void main(String[] args) {
        Thread thread = new Thread(new LongComputationTask(new BigInteger("200000"), new BigInteger("100000000")));

        thread.start();
        thread.interrupt();  //we have to interrrupt it to finish the appcaition
    }

    private static class LongComputationTask implements Runnable {
        private BigInteger base;
        private BigInteger power;

        public LongComputationTask(BigInteger base, BigInteger power) {
            this.base = base;
            this.power = power;
        }

        @Override
        public void run() {
            System.out.println(base + "^" + power + " = " + pow(base, power));
        }

        private BigInteger pow(BigInteger base,
                               BigInteger power) {
            BigInteger result = BigInteger.ONE;

            for (BigInteger i = BigInteger.ZERO; i.compareTo(power) != 0; i = i.add(BigInteger.ONE)) {
                if (Thread.currentThread().isInterrupted()) {    //checking if current thread is interrupted from outside world
                    System.out.println("Prematurely interrupted computation");
                    return BigInteger.ZERO;
                }
                result = result.multiply(base);
            }
            return result;
        }
    }
}
//Prematurely interrupted computation
//200000^100000000 = 0

Here if we don’t call interrupt method then it goes into infinite loops and does not exit.
to stop this we need to call thread.interrupt() .

Join method provides much more flexiblity and is an important concepts.

java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution

public class ThreadMainJoinForLargeFactorial {
    public static void main(String[] args) throws InterruptedException {
        List<Long> inputNumbers = Arrays.asList(100000L, 3435L, 35435L, 2324L, 4656L, 23L, 5556L);

        List<FactorialThread> threads = new ArrayList<>();

        for (long inputNumber : inputNumbers) {
            threads.add(new FactorialThread(inputNumber));
        }

        for (Thread thread : threads) {
            thread.setDaemon(true);
            thread.start();
        }

        for (Thread thread : threads) {
            thread.join(150);   //how many time we have to wait
        }

        for (int i = 0; i < inputNumbers.size(); i++) {
            FactorialThread factorialThread = threads.get(i);
            if (factorialThread.isFinished()) {
                System.out.println("Factorial of " + inputNumbers.get(i) + " is " + factorialThread.getResult());
            } else {
                System.out.println("The calculation for " + inputNumbers.get(i) + " is still in progress");
            }
        }
    }

    public static class FactorialThread extends Thread {
        private long inputNumber;
        private BigInteger result = BigInteger.ZERO;
        private boolean isFinished = false;

        public FactorialThread(long inputNumber) {
            this.inputNumber = inputNumber;
        }

        @Override
        public void run() {
            this.result = factorial(inputNumber);
            this.isFinished = true;
        }

        public BigInteger factorial(long n) {
            BigInteger tempResult = BigInteger.ONE;

            for (long i = n; i > 0; i--) {
                tempResult = tempResult.multiply(new BigInteger((Long.toString(i))));
            }
            return tempResult;
        }

        public BigInteger getResult() {
            return result;
        }

        public boolean isFinished() {
            return isFinished;
        }
    }
}

/*
The calculation for 100000 is still in progress
Factorial of 3435 is 1878776836011803584481195566731971565661498258285339325192933679402021775398045550896145471932565999960444319221519027879657308736270470906224641297627419498583987894322904058544546559212714073116749715873391461495327540668544183318192733372621335684863110321665450076969445332036518131444593353940193465289794362037453215133290696542139638156930142523436941904088182017263828313554844472531553317099025250110933950638247374242593792342390630803702870731961395656012210892309200945575031398556961925087758236575409077657375221176556239314537433354207086990800652917451076643957700752325910254290610403138131373079577875460532747105374639485390364014543085996195957353703330106930224576077242113214283790272399326975894937792824747820174319882988301390414893109414781015701057002608620436986506843595252456736038661019532277940749797169999902981524387746815347130904269174752469626110978652864810632481459070750088956114763722371111177286352941283684772565746818534499464820766373043756185967166140561502298575745958576677886664647765326136314030932172917244131986737885096807427549924355159958104069193989889190517702027832542327568232461022002021798965596899931968653796855016362437401706391392998350745995072155535556180636380683387977280562481469330479583018055158513194867720268661830419510896362342459059425249554456293093065674970211543896015788342329839128638938691599183803050949563825863915960344695585276639683973096460679789889682783074238198641908338336246944381908379921102222103940240236652704461793090038787984202933266695395779371032240943639489917402470735577871321296286826222635829223563335703050649270449130862718646964997561319275927113715777481474658867238848560292096610284431906034552778091918658212550454572737283354879144315559662830022046371015992490212455794085347386105731421189393895516112103109914016071549315707327528885162401806108320269737006147331802359642748209181559705470360550581245770614200587817019082873367067249781949850066355267476065638298672946463058242625541835282286610735752626212519048698014578196901982864173230007648201811166284867529912699514515661229057426954115587075884461544410992388282834728896093121975981375592404510706644032440712461435419970931864258504979402461747988708435496342477647663681137656115985552013199897930618081700636960377079264259244237843806383070509594974957496793489941074456361539167860720773131522126252528119123361030619220581020738244771908047293125611187130191775300940824445496600330454912238512685006187837552615263952857445543308631035175503843956563154666836467370253071508683957787149322678309334127183608893904308651191039851753788184391005941992690188117321476709783026595664581705139513766998008348376251437341983358026531808617859619323030551672630225750778372269206636136472674827124261861316962432855602488931281689873776308798495144430512794173182633943473937302471698726425639819132397990374645534385073264661417094229922030114949748665948124081764784764502351117374555191064569421242809067969834941318973589301859865372000358837284036257160396018472696613069964733980474545438372077515194911925920535753902141653448604670612180723954320028330371482533109261038353686836610114524747734460269300342730638010732068762713640188091023965162024011165294316489619243043466045680416211993837526220020961228357960445008730276652015051556395028341745320749266161325920317670093736971384210939632984812279161595390091177501441398670279482346887925570661288051173459985303749263664622052998275766354783629900876571709651776177825574056812482558076518364374432525702037470543351486841526208205793017395386517588954079689171738777164428089314421173946416216005097045292831514243637301926364385256215649017345611785728237639925838114713951668648750755536180615496335666358391056646555219906993202547591124030010728424221915788126291702508533442670526677547580573744910631046684244285263279298740719311086746566157033188323046039907129477488623743621870380493344111773388367009229291521905343899484344185005575725158275822074584944148349351188948093112110536235442648829334493793963615655087745667908661198872051485880291684240002722270108359645922847682233083717005076617526790919196853596571712149668299731224556748053805014163281551529945237783048741602757430357279633841195698573206790689184412326454013413175303767665710437955473418677144838063081968620437206941959796872462891206988692623230059582433564988360605833422284372461066177662714194780632285987697763853591792098163177017668224483078991726949798174396914188160582277726009951297047755556159876989280209386568958687609864225808947748261368659424748473983282922520042524842348254579185717747546984570513771915482310311103207922692126190759435669270151028346758574246240072897075823510735973634834390575473630378793117275067556870724044245085977231618824149462167619410070640423765884327593913081695342414399640656355273896216516173049448717872081688976434624560431111296432025575908296899816415705546587810348933686051898584194714304034177187636827540961800491795224447824661853298916114846507940558358578097906187047572156307868971351016372508747694413878677330035365597361948584472349793367605402937730307592902882521936123952652915438476977945532194368394549154338442023323257542589543429027398801087594786149082488540452020639553723256832685922476487861062608396232291707891501677332803694611171677421784241698292627321146348085988741455621658534765684843518022229932051884238765268393933862035459581349270292407836768988293075589544357642348893936975529396891820175199218277077575348444220412286676172311004962019221864618474988715586670324079511456923322479987286032044095761195251352989767391970209632181388296281623583491306613361780898559537080078654670673496488238270056764638425843006147339453821695816707042740685438101621324592843837512398027232671409627322563161557244471330625957246426346115371127739121664090611257073352770051599441496930233028696850065997120347328908028699932250270009921246459939413077071577980605060277604895251903128174092475776665247474973088041844245009370699502268630913226539799261149994978657093122497562387045895724277791753450837981349099297645595283398700538156487871570248486269367182736610193730345599126613802836521029006881955635540812306430925608824250391727992907914348459126868829231788265053356841185560245542985887784333416788985232329269590763856223896224180576167603495185561995483467409931459553696200761264774049714139852712859900864090195860701407687706741141075116909603944747872650899457516895981967644041853937663549588473551146853872799316246337657146714549226753603574436848981180615409617454363554574978899196978344613163674664761346726655680114962938388032387216809954225533032805049491161326942545851401207115685461252224747994315065195364720020454433460525447218838808792548533197249638864933453387691069770704361737532062442704953292452106715284464009582203169266069950621973561146910350846845070823294004802197385559356040698704423901610729965779494833789661304769367933042363239169629063680939203648457800979308476754718637647377190535681429171279303025426521131053406127177370409139207095137863994407551463624091497378609360898250469410507528021296697065051723285380062987488625515139142364269307332560078724348906941692225885645438012708779853483131613787624927132401990644503852577703523608516856405617432369237775695186164050618043463848758185120453125898684441679453968276409010245971073733643939257987265458262386325268122513636649098202225145292491586847559809186270809181643126886458530076358212590533969895947924605740847592182105217932471859240067166684184695286245992401401797940519381101700399098937798990828279292480846076619991852060219083480419353717441296341332463016612587257943340352530722532433968653442672795932177889036060592434662318548109821182998968204609365388612814966050470181586794920564744830923196057182237789251754552009945963653637545888518833127351242949102122997513726565632043429218121957169073720587182875337566349470508704504333452715761378650385824840181698474712489098263163411720780781543125136040804038414630928578847773159057974734365666261582083094115339071259507092597955383324719275146569042556035211926230244261051290987687853143718149458466383741230763383787666688313493239542953398828215162704795881981174610012622365578453359357717406406649114651439060837999156026663768104728371819690101143275371810034928264647916821716749104772074199067684396917574141363478049423210375130282184291553483320297208345686876640635801294159567130162846538356788866876220544081853212943673444720885937347523669181571811203913490125956594386151971364293306502830512759198150925737096438345801571553425351689721572633026927277246334618915093511782961669765318862985973968606607151722501041872567746745229289269914700015339610053019640893949965686430494955209750911374335239966639393145527624267465814679976503399655026095162655136151596376140250229710104017146385242430642524224684421742821704023296441890155153915617575199866189951610793043291438108531219508081135573644979009566723198923662120854460224108437313567661203587381103416005314544146334682181009077448273729999258703583411557943680718919587600544253837395524322569202622058748669543172307314420231283057303448550102114290071647500414259985022972436554397994946339865742188741598940628985209587095562207166391734055302209376230219205118556307849784525271194276443952372568368462268113821068991703692085797498447104265200304939897455169255404391370133807373755126066470576560545815046942019121641662760829311287319518490536070314543697848294300553150084495408835925511897641795881078401418682249228176817873617061369095845875091938730283990689552598644520792691320816184798535734605287146715100999718921641479392138344486947134250380838431596387995636418124676508700934403325711306972567961600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The calculation for 35435 is still in progress
Factorial of 2324 is 821484071650481810568272005525454375722239535835098836749464580548444469081208190166638546314032955071603477825118674497647246137290734755343326184120466886192171158317348985784369915907226843129957759943777055455894158403299900114054565949730891946898319371827014833461223226832809258213455002671540276889456513662359052055255621813372216759152970497237764431225885804697880060521610838996036961117801752963142434963916641612929356381289121336420298249818108119333158339116559813806877463854628822918265757194314991968173627345889611551204046316981657590613573838339478086363236551178704773584084506318592391821509233410905577979705495880136773872999826007453135540830747837310933322486990910620017627199848610071372435103968089914746031177116551478574018698676457243721818889012540237367732230497106809426235203119264832067502638667402865560417210166698712427179853704093651474891997457233219480004167551756061917060799172670313145986100707185632969551736439129341815290126349092471360871049770872876214537118589787530994822636245545312660313843219203307472432618443470892359745223118085975768281908592407258897780334097913002960585217399169453046875824691608568954978557278754326462876480938501517947464184213706879693308552977923450212378365998674820823807404705366952572567587133433927896413750303921987978925649946500538789850966887633116570727120575873450540432587615643781481957487928516488598626843478983694739219557156179535639535648909450856953269871886530331263164581367846352970860013994953000755256913370518589356219068119771165652727909254740400232792813148776118366868665602044348989480220075963406419026653443558427378588054070191724422889210182584331617434759710921522764709482941021913226141426600246679994665101694056852767946820560099489986183100440037351257726804637072813824244516665937070782064831287572101764522432385196828744156457193739783385116085267524190951437388676048762712095527583442913309503997588795837488485271039260301478425668349512362267451302795683471631734313391619881626583917116535870606620316596237744105678296412161437652130176163928729240408377831834918077961943745502120141515323624695278625083359695300863105919129104730372917726823044187418961598482320486606305771176032276686274424357981979040998759613665607748912402683723311790632659031509107460243436975289692178872923069167458045726157636675265145279667603586943703949429848299857667924486573557995726366031820376330229252092303943388805800899577685746942174721176265870223596980237758156383715981425943100412088255019640433695286409024549242480569756961226228655429604192923724390754805691024847172182505825941981844878585660273465193428813087879460691551066645149273850597608923213483307534918202671906475676423178709066587020714980287362726380299257284304579242178748152981973787528554169381798419144476235070056513733139431349511795988539174513544681097492472079305278056960570833647246430767451216710632901925301126749913140505456948286570197037712415701308294461750988145067773745954437655247251819750244134608775947172349491069859503495834233487244139678469513296701248523637926394300359211373128212668924308697841815269638139963710200025830340212906782059669194733166051459114122409479079073839697462564891494001200377758901385317977461999143044364625941930320534749302698778718896329253298548863966431166334627296441119968057165982925440465803010092630893424659396658217080557076237820205312091921394344100543504207256051677089495744653485781862795972642139426110604333500539881196630366264545425902610693486554092218003473255720792468139044872832205923153078263671320578884458110741338046350793952649437046128313065846099692512472268170061388998062646708335346787131303890106834179659230059660098351940879065501713848763721704584740841446095466174357576204722623366784580607094101616423225766139905995430741996578367867335379299680893402497225366138058201988689907729348863766915751748005718438886337009987499685962620087937332011405217525474047697895460400180970287994957678470650306462077843300048134702347677510667226167990950445516973991346221882246856265417672277664189867344997760064753316045140134580304009421880919745159419689989429242068236176878641003675412705986148316831821284561422729727219767175883535963613900047599308499006597371640816838894092401326105371447588639032532072355452248630394872791844950502856184347477055947673953182879240277500341255954700107015970971091145956898759790084435519989010019408305565462030455139896712705210773468480144874613974349337714584642114274248803921363820372058851261086225775577018198663864605870227070634780504120329784437013076181386395796122541924452579711117863136913520524571184481071676547794370555478237615951225860080661014110404083954046339571460784324320291485392246731122421741674848753693099351994164428187607728509604037793296863522451851737662827299332749086369134818961212276819297205194142577343165188674044343024392815305865831033074499758766985659076980102247505025899218952860074189084434225012188803599293844152637757860501100898695487120712649689875251391170782362278794606034553818314232015916627844973944954250974554104001621938618443551600074171224499789540199936588118141585806715318278220089504294478286666667116261199494149046438232270060060483887530734473092442448469155486443673504225230817771595749965378118046858458511253391628173752701928835976076454451068466590381982166118039031155376777954394115438975168508847009449539019212622272896534491166247021527068655144472087328239129389895511708389629920305844255384393175578495996058132076275907099708740028857230969665114226705510695409213706731125934686031385736560902507534454170572390814583446035480921841655687329822571360535545240085912317418354431837814834034939072388330638701849460545079328275964917278856535116826361151098296437839809619827769713246797505504946315374664940602304256910090314279947323810772019200101528156717730726190255166349297223491421176168654082967752697909118751593152766579007518310877778201727675245001991034444264994094809592553829674387552237135740005649346818209044636498791795554963143368290554049061843479915575423560302731510876920808789093024391964014799851699458566365903006385203532906065643421306968155392668327692566120718817845844564693473034240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Factorial of 4656 is 26692694493938210554867558049502399005518196600127928168307785835283125600382713958108660729459477906388213209685407608080494572018823788442112066398752426023586837642493799906371482688198252306936730395531105262037660575572714172823753704535583868189736494579234477411495469258372753897558102016142580686448869359094916017918956236587694601561728718289203330993603537283207068037079658427741983917571626580196778550260089573188571058641815076913626197802325983420668712701084301791419325248749140804850138461175834149942180676892243388177508954599925136006425419515438685193118534553728861188807507472661594627739827025560749106194399537292439303948351368161445294191410891466868892021386338659399133635607765563516840760953119160589138634801843542860448892464419364298666582939662156183420576856033751109493099473089465522468146903632989773085200151329974073747416649320935485638920822737278761426965972419747343180933902898261780927728172529533136916891600626893528592366636855156424026647135894704179621136477899172660443498211027144175419046239367798874772149360749241227372322501875955388537546722440849100866098288695095349982806707957822052944961063552150866546944978070189067194572006354749938357473156987639434288002206626999030073730953909432806791774924404482574077357195552311085567213334368658080937759739099265043255779293797550914622861363972647382114227777920009090112423891748155158312104497774649403183272511302028312792997600880042909015622729168802741366431450459595051311821918251939386437954787434619670940652178720938441798458089962320986671353779183011674451073107056004653319011085057761506299504023955973772850830560703243940267921565295430665384969715774451977455121985142346572072908362641518818474057825707564817485415449378620608502061859910553458740553676607215634797312005218059218274964398560589097734762921877031035849561906892045494315900290733929704577116942687614284409416500938165715437706598362183720956974811875072189329394143338358816616300993441845834443876071739218729810800970081463757827236217592451092244639944527556328999833943120644921866009934056704736455055393726788478035854658348125573761728309911919719904219951359611912678485347124465087592163566645918189818362587358506348218128062262279957996600480224155955884439325585665088484387369912392819750366735811742326576379868452984582033342910141567795195756510107964704537650807025056158480473567992615848151874892504180890276658843605212994519232992723656522569338207408024095764329337354182549761556740636920086555944234978199086841017783761779662024514652262331525249770015367984476952784831566857031252195955823432363984804118102467330614384250863042579254390137613785075084947979887467437848069539299866552839496015890373844080632044147253591193408618324890640895096231382223952243708572663244868460017037286521002605046475373692705526481023172463625392924304779861827789206196759449053335512923923556277873870296131957872334657615383972642015919792076954825640574481765365702584708447951410737194815017988539864081559988202727565705756321592450301753958487514854459450494917782182431156205999329029462780605903247651534473034976862071943541486263021420548632342674522866616370954046930999948393942645522559269573386413685184280648937290460554850517149467651056859990194978478976337709999283366288910407892759695689195248507540992728400582297875712635126335031920830907655435818201753442149704762893810235074754572849494776629030680809123351172442937789615340193726961060161741438949128897930646974980841901103784607572290123823060735485878813644974261356183301609632912638186288340780618141759891897970381314097912601947571042976687787135606804526956305643142803453182838943681579369393850415844405812777195966059070950011942191601346738547607057226452038413831210364117735215928334893696056399321181287772823037461312301855379850857841087893913269801898931928796758411513251956968472513613948123628616265029542808588401293466715767515920764338494190485473557362632196169028473112307597709171445315723345451994596492156586328414431153823111384598215482105839475282301404550874209799552907184795584280160092432046521459056379477473089077818944264854077676044406057257651480527637071603984922290961352806626751098184904457539904584098775430883160960443386258981391775826612937870017860036057895509370018000715804205498329514728101729390802119020331832566022435645267332141148663261470504940884965774980675277658376330000386200699627689139922271601343146045867952653737765462634858941586586801802676578068786163943212562866258413587240142331980762071854000715376385913545367466216642886013948814871681056549534320524044624286659633587127995182450567355084902099502768431366171381363119496470047142277460043640304805563194206632854354423380651035345826815266513417117016522093573678347741200816492531064898085278008087905722359577686759041636626771263329651885578177864569587899426823474286226255058199324747902737048182757925895103860271982297508527496043165577386468059587448887140675705322802957485578404369967189942142993252771276825887092886966476882675502668379151613763316374570565457542296167180381159886775808163795276872634493133173948257172063586476765754446827490409697811398423765308988381352108411057469522066726492180870710997222205020872967041289040981407262805178993783116405578705856294318521315413133906205488178832909038317819779225067781841955331316274481351045593657430483308323251670245536124748536377523665046000799727946266514147750279327131983805449607663359338852142767989302787977728426077816045928056529371853741585658049717833513982168721740924086749550910845938276672360122376953474112758471300688327580830484797444320281655651380447510123190588434465538326556717587251340209180874710330793240565544869440565248190599952620383678868086163242384748281974740046578215169076181171287638620456224441683277774810848042745560145952374372870140126489924942301651095654139941046117680406707677269186491398140893144648759393632669450152306830133784993727221989848014975467621194853798876917149480231120622293409955681183259396499751681776422571845727545299283118234287649054001702950759325251941611698791303778551455826103639261019871788400682935339880724771840222598588236855258021776174799740384152206078687438354503676476539536537103722490851712200403502794472993610204234329018920241303470833578293472827722529610760362112193179507627494052365099644408117964105179436953501283206458832363571913949054709961575158043700068899965808813324895869744398568709992927247110104645347424269645235556291452404275134927343321710573537970986525187867590511227046520431584917500720750454633562249367895489391683350960638324424887901034316170145976655706975230710783121991180435150345091058379788216334150373938653077910542563475138052596205807408339128885712726754971354492873854903349109195056260362363223693814909543098787176722721029293054297352728247506617294517546191650774566742563481589595543020578565766647065161974640786152006670568301211566310244525113708507471706292073768959166864636823642841730051019053518491261203552799883347415850823413239222786126471483923409745686581428131510804989615258065979969124446998766472869875182726745089501291825961776873594399241799765385530715789635341799046279729833142235811551619503707577475260497804480997640732548935882898150271191174808455888189872354430970001107078505156545029333471943931726804207079561275134866466589105406897018313052317661136975256327607136712648276237224766443063070578622038658873823424697561611344174234902835171162581836691602364329251914813136658068263362264428429813388955917929854145882525440775003881941031427768480599795214070078613534231818881353750980926539133884800531892809390487470811387809924723733851883232679213579539057531521610467945870572483389018344015333355717153534000836642723123326217969085572610356221987055822590472837726481121686799785685468199690966853755724126387208792101438782599931143469745904053203214137511216367754542617040698542941082117645591577349542918068410730804542720865066753117087036204087139036902611882783709601059327169988195497971426843079925664614872319735963565808614604887734036693387622699413296977536737147920001521356457677522699149397226741714035266200503505119224417432144098087820548943539176519629039420337441155913057377716171351006281327028539700034236500358536294749351948785833069782361386165714174684009618200346742006286031163133849904435202406035651354424959405918566203269392009294889991753761719037518407700822540883935306804978476966098714020957637884794516708780044910495462299919460119377378348503949654217010364915312764983082155654549983455218777005171870348223250221437699306193894958569175106215099709262179380848232762522000146420349807081416398907185146898527303589847717489242564509718838884584267706473387599000326891816851508316936784771684020220452460997926808695985813276532845605446300727462018262037730520695171787441793701956891110054773725745093166415096876351814732887136210347523459667920262374003435516396793753816781481440119317963862278218574278111546592107567363628921805191735174888454619914805488611838695430550014395810729836497031951785371392400472077863482427320404628518534411542575969902567165331068663859157875909964779835395193587950703377146342142199875340144467474060936813745629237710557416542316654040566769091889976050831887642676939264127776447232347875237793160439335813168652320970327472173547385390905507576416532292838488237799352916966606199745570653965951983374829044987336084238119784440026480148825858865313924017092663572642552869201168121930321665708951949022021374813745120311279451237925926238277734929278506688312683784173195633154048265956653519508447293894279339460846787914341502825110921026400394963597164966404731930506460570337372499408011753627454864699358111832057060245731224009098915364452089023129775977827749267938926928982365339958370605725095340479959673834655631755355957198853069541354372018367230706339533102363627725502807003699167673121774404651834104392473533642260708031843977022249331323969556948652251500326708362196822525256186144615570242002972665134418190355448046014578172881356486649628969141568982619087780116563755541235484636052390239189527480762062357288593893760869524246344756032087525784162817348323642980204073784918073409302384059165494205495194920466541223454820316578582857193514627356634128995346894453269597262589722800203559877466526369597431626514879297736455364685636579836144050027529427319377311526163245430270248670256649729457585110642875985377753747715072261533271402340120859295821713726040840545795380097086561487087645812441665737850497069154619131064183204195138571718975245987949533623728749158452515369780972580391702289362441335037335568743370483136322276645188691498265258873193743718818661755948864496626282225689542561959557627692204331903368462783342819891647803055632669734039529633002084109236235539836814186939511004188274074385214776970205614201799081254503462963381720480653630983901525384343276646235263046885270433546755291341840040476705890560290590716032706035678588857210643010056233849369135440811126847423771182179366609545227503577439013202954215869250680553090472619233194500749840646651904111836356836561727280548702618684823245212641274181755140811376153594850440477674478266750737506204558545645944068314827824631161775015924969959729806367268646624999228312076775563902589985129957907967026074859343625504333481197050828649319052174928740098068312808556902670481650197483584245322953091344952118550020666806996847056618559866371461523166920305287857060735783807821622799302492332800018494117170801887048889403040338929034062093299575148026031714990321363793031797168140808040407312464941571916319787108267713131469224879735495549305456620287154648411829865165501170656840521585824682210420152595244339474092163116752523818431207879372651124473471439810781915815298227791307636406158658545702090975413464082294207882549348194983161627182107849953174038455499106783650539661400501535746203030252744400949919227032873924749758588027568353672275269417004432065388416572379219833833537989024575995656039934468604377703059380959510876580281079571656298053009008621005784359413166374374314171531216001360813276494145922360264251569953348039006642225981534493627643896751146627483422167671834462040671943526637438326244909830973309051398999124734837823800579423458555392762733162175774553454037204346058222997058575589882130450327527446286994571001244011680048717777955488219875039022318631846706051023518768951871124117845202947990320477901479898539556790882353577326312600743916846275533511148304085726019405454110824060051186311196217864069120913179364931598919129707435392319788157456262672408855599792965979552803309194214537266101142852401176997394801877345858192953442658676883245949599188617937006128386121704799634556876576020396013960732146286529485648136984650371777381189480018299206664651669817926782614088761301141920989964341764893638478709842657594010492845247865559243261962466859993353413829471914427977059134640263668577118494907014977836715162633615926730995202898154968048781896188329130635857514287316888919696790973938194611884766535302501539527929711437850642254873715639541234487892834260359718827728048556912071310563899617140876381640634384680790647598919485407879517409806393609810702286178124364347270023436978733934905394404229393584165257608814607784342423558655578456543923169840122259804401332115321895884986933570834735228197860902673633533562307409414788915091029472729074018236864766337848610613726115014697614132228527686147184629796162105332236667777122002576627077123170153922843505507972548396958430437464747058258615355857941596498993782903386993941475826456406411872352423763520574752791180241730698407046026503258860454220830892618925893340937422264685696922744858527083016289207361207060828215669554624746670533401694357282764125815548478691227577706995282089312056770560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Factorial of 23 is 25852016738884976640000
Factorial of 5556 is 169951596443651975670936106905268009726857840237226336013137746655786102060954429703093586502331082826160126799371961047247995144715820070247312688604842794768928847291312893896032788114634112357340801046170478091568545158838617903734203035837226035774546144492055388045581638976896837202800910346701805528345780346448737279942799768285740123441113877459743908298928378568056037832672886126415979533891158448523868699977603108952611899206157312431666507530013363132993908823345469036140849433930083908627837996081859089633868991845092390413814366502910303364258473646056963644417904925510079733945695273847607353701001957979303839233109011259150434755085862227371047733279531770221023300431223694678773633509783120992795251134935433303600096493826614284318652162220307491556740982718776566609669634479326884975780836645899251468719034078979946703796700833659276719348286655351002034583924724660441367402080083004659766777494283052777601192072947211204828876249280839843090266089577254988773668901170746466368158027524056266854942629776901104375214407771822962679765794861510440702287992151596790417056702580194755531200365774118302552783542994947614908886621323841139149136905270053165962626883337469881623528374092981297320811491512422804215922176107553566645222634369293115480449684278646031648470315974110656135897117119936689167140663239426051203159415061446452162085232615926793971619371243237919885795620567731951104874039570625761069467078260505478851321742975131890641372519244089290115790076615548628505328877786045112333515559127489781749644061572941149555063425558825585624041597724456759955192578012099806681981197103222463932046307267161736948283096922827696202733225271542717306116806768855614076805923859634261965877464640651051018748769785899191863371550974734959997295624080197920604195253638703752047659067197152717944878053870052052670452333834137582658488371091066516144366225297018935259644696623758926484494232524357141874504029613149970730385686225732992132224787594231291584670398886648633990543841242941894309607103686902732067651793799088893191373675821844219570281810507961231758794951284522486585924358067441331583880416704772584964787611955079424203075290815131583073553125325743268785756319122579844537691799390859044850450414279628953589179073957923956033884175730938819143780020549508752633505414096190500288634994309017540044233137009053721900257594658528214715721339522617030650434600496837502255429858948832844413756231468632402237889526264014901502807787079606400561069304680687727300456713876228624695100010660344587840799344396305090333998454412690907532288429925280291207418609055606803216182586999215333834189953718435255282955575054736873663059092116126723853840816368829287043212624078831291897123335295913982906902503232995930740081415502291676740484476932236926395010403588369557749285880963785218816241505416041443361377993203497163276680025989169299531424153107153962486584423851737879020050697407622369701595487487055826936904663705595996327302437582905206863088735357469915185316057294081800265621183747276485862706036013213731622802656341669233244628364187820436060765060921926229164551887151964789768926560518600687531858445412595337424049714704202916761683508191587154694907938470059683470369316959701285517233532290320633607168262305673985626047073525917613512134581686553877943883117600364435760366089493072611829057864961950011789080356972974544315809092514907511525812841647667756110781511087728264608227816947035574492838075935770575250528064998435910709319285379043551829450932219788077763938109995419757942439215860006545108102360911754590181154734817810025082069430901330467235998124578591996101378880066801514055746848709489147930774586462778926187516005952558665216394202954516402948090585983609720676195726962781554688094593627225833619249521345701588950700469851944335418643121668140961332365083551878907700202014028936259681834786786395402572781843016642859607645104758068877270556168656679432700380811256940111798771148487017645355484798662185892063445255128420433007840951008125478194931000767447262514782956597480398265254612040615285027810494170227710336736445461940959750910599894792877438375503463844308635179722662297399564910219078070345383059347085380948461952690705620160700988049647123829396015077881872712217728541085350567107830317390917558851035832214773248750670884434558059571684229948611888632747504413473618951577094763610145905218356436149465422171133936409636343335502831941955947444801444481398486278583998682955451559262301138275711479881607490607581638271866646818694488739668762271728748036309571833385245654560165648366973454959847898527193875997710198352537877966107394601201351632936827140722025325312527397610525692492897605588347649334452329730702313946554469625038077138467087501419341760503547370849244503316371064914891356975943456649690965240563144768786182576342763144202528773628679114689966171049786862489513694133511911829163365326555513160228454986210317867952925756072270174606949579602413899649048109232155070423880860104835689609211005044441654362788191376795591266045764797416809012899683193679421146118318004619628892522315350980838017266998207561856469730829513998435340135487217418187621715855253156139967986415818849466564723549197476670455253354905695010809781663535213090745769977806034757357666978835278203915475155802772652293781147570252185940690652070290167606938541262045231749560042605886148513012674916263544003776045940088532554238558420588525097726835062153119259468831138496772432750099350650681463989025011144222048724413683533414308235673011068207981684080919243477105415177743089741705656936946769574602290356896202530596586798443054765877474974706381428823975697980014241124457945125802281428218267909300893006476531892126635453625010951230334834923259460082477747759757318638957899638324376978362458108081924911927157168889486586500758135574345207220606018681527416084884650785612934391370133978680022017518429062955933247483286296769112654227831497514797547338255283638622045112438444241178231298482643267672623918513709475009052377673088248340646430977887534004276603194043813692178192693481607547214463366569788274174539323786757289242812012439597341631933594740505597619621175459315253419220618723284456163461385329619329546319079742242893339363748283436842664685166048333381768072835355167845664021450676309675435000528918691734955166161502641729826402920087366894094349586845062188873550728524757466180918529182974782503629967453716671155367083537372890350846781248844017965664445022403476921974788281624405009229698971695227305807063316537401308760762352220198379862195300809072822242684847700021630045568668898648381456334266602395999692591570470509621714158148630756607261201210196728938722036340456526084723052577350598686851368908000985075589665455347555274033286129989447677473554714718965966550844150582198085454572416432548505983611271058631091635909827488592723279821116978367686251493822580952829548949758787257680932816019153062326738550018449975236251180123962744408683948681265294337959391940578798847378232987281152471485360831653916573330236129848048435326007414921471732443411561090063288886906769729650091250508059897416935666341069522923110602326934445771959447466348334963928210597922293833207000042707393280874959795192429227717759090907626316556325784421719839404529523593958410744734531602762196091937630947288345715179579295344066287806381572562986321112495500707224945780843142747210111567051758763797615075694451383637081539253979385485346088192764256749478432433467021621872827713971123002169034644014010010996150985665535029484691292063427991095282992718860264882886425574914828366597396842388511288721782604643069484857728039691268907226782454093729677753946832974831570698485370493509350369230530889540227351843224633648261313443594552917601759539490052605176402789413137086516655308987484540067236632338097354337618956323970653468135357718943086497425687150877935353290923567508523251887764355194377586021289905794825401534689557875488643370513025882631932564400773293809220324239181150924711785152385858219654983081971245520524796713915054430433742448844767949772936262237604374935314119386559110530093770512619063342072422802676143510611945305956242820920206179377021131537188340040650813215301047840158142838427225213641101678251399082446256875757699251808340572053029131821889193062006775507372178503541542188829206276883227142279186186114650570847544420188730085725501790231362532485909141839752067288188905232313808088337431269060502626409073444876821209131430994475516817450647082655517151007123683614391037556377053632953437976410771324336786868062653236918865353332845810643215878079269512110024032166936010400136341201463901504862994674012244090533251050665087818006032215666739608108196723725281772703499665282435987673478018971976077318219653545808129088172914976482380863334529792693945145955295135792358796924650288998074670204873883922619366356125857013535443366222376470184508191078711531380147486067786591247732052053879882492849248275192895498746358762394217799949008188592714146000910558600488464717891504657486168061750940151180371973511228878916621996934211722753789738232969382275622324853102153679707383781282301088731758134887859998902932555152019150849963887554177176320820321904382819690756182966691063893167048341032340784285932830714344755207382105237357668702025353465600315643922112120185363321052443162776308540918638385809045259252965174356090452046385653996319074169653633059347294869416141160134420184665314074103555757292823926565528825210986700867865696161585464495067036289666412020439065701602457095291377406559521636424069061330225607862003009481420171054195022929859884840235109002978790775339964963641190425165886644996154172482070190926829074737307550453714275559899235832817345654665300274689313685401110363713237206099223196853037196457695209780285450769559977382820246634372833322063699118435311225986559536823127324316610425652422464016394113213624260370967333115939936541980550765448084132102333240711120460137390180280301916065566324727100499293631209806410248475750528482917805051338944004689366491926769027309662941948309749752008935496572692456164378085956238123725101497981480197026377775488833489489788450823061163162482903356463624051804144732817846926950619450355346033156911776853333426524175662404690557254611458803277309938694045055068692610762017366198682456463666254830089032022194193012172013395750191675383891391095768579058377282727204614412964479097702743368630881665369401410737477133019746849015068042649371238545865631523748059373345187342583402126968727241583128723928004109928332851820946554591003672286261576511670470980896398282531062134132380140961641008047349676165120303192525481762491797414973432157037423649856700333197114702154446786308357859292241149263792668229311746387162301139909965324535433135849943369605103432670695493671715367034187096945649554307957957609486154739852618868614036915135635426148445761475273505696972449895970088625081695359968866172099090792420065960342073419880830841911993780218904425453628398376681162289232732286727796313070470269389688539095354418229796973852133363898590535611163520527845932058865433728265284048972166877419339820041259230575823261237053798441374269391036377706128861362212318491706633381352057239662894514453019858802427464049595027571155643400749604070854390631175523196824796452672648377160373000917114070997970879144729102435629884018410463402505168718610964767873956454274874086409501033427599152700684633965979080473903734320943542207191446860944056834731493824677263643227017028787928427847049913246689548487679595996675750440906799268839446115084762632028725109001391015774368464892264988661538904966828992638729044025723664160268801941323684299122407630158732609179760901753194806691281935384112010731526688607705361328208050727957991537021327130308287560031379143446813521338231859217896055496696081101012238622891601993590478161120874248361515565773725546643756364974250652878535345441926526418956615147783296673580616556584884319059228282255366174969510450162945441380201700336166741913535198745434861870092973064725065906734398807098385770784349595868099454890308294980283498090148622690796487311592320194641758363895872843864105060822307937119497478735108041117585085496364643653102175693046992946693994001653527940849415801478416430527653139525096423143773481206320496875119478408377420371872624123080923180166620299266682335267116969003860848442238637465302991928154379173101767807362791456298631360866809878520655877534870646920462952636295639237100954899378306447886151254905153895619460325299019152356658441152906186708554306290915578418887284538790033119563553735809035370544749990446336530503695950334174822116945112928035160527161470700215342163788641270264564680679744679839196015622417895437143223256235791723510239347768086409132072026692433617312487569603437465837274039170721585051719597972468590965454184353821781774565642399194731194697249950827066449632256344996485056682538583000437160377837634644953343438151044515612460575742727248323822793636140062029532422835186671847619229773181060718327094335293346072092910255355138407193740124424445603899950676630080490945425482422731612478694213199378883573208548200924458576405229972625336277985082877717645670438413961704158589851392896989523250117649059563244137080237889043413960726295707833288192807987864269128497841524704406975251951996100841483608304426363628681139968539012762074437342239073333545942640471709481135593342543517048212927174956779928371090178171998286110152681646141088164174285851807236402052373856463127297982933556360966315608288938291982288669936481303986313044569247241558179253269228556550890943150276147876668166008099866860255762207587324241987659456329610979223659785093791240664526928952900376345439714074841758291431791730243871249310103735334819631109480019283221429487753984077503116225629149451088213746752802719593210834704194682227915243272510778337483374701322905895669496454940654365859838787287474968703677199382531297480920337315855716253941867509697793736161164861847860893696528022456993361886553902447784436776558121007893274520477297822095267305483943815103743102906073364438331190499434659728521318622115970528843767759552800016408891557741135944728461594090261955053749288980819080965846236414450727989537938125555426257881516437683528804498193909246208558764496357949319805953727093692452537967015249598335787710789520696522278585144881554115878441921120053686165876053301096021767638152232703083951445502448813572902324952479489886884433541621087574094382890075125564060846626616853884167623024274568914403836835265713986583271387161896343905463152391824161446479714698983502364439977875931637172854980752510316189732707245697007666784279478451142455533737145116042952962160446996683259155465515963130881456930362589534025312784582130434287685388737372843896820477945658440765457110721352479782193258823319176205848275193134118337315190854749781871350227397386652787988063293166217519500776284659668495618280711462523952905657734739225426955811948464949903820626388662154859787483138785114366694662904830964305446536403108675135998103598554244969801683131348057093460843070872960027064300167436415724520361742676265932275025403669719042426110954980646043362643775994676589118240575447003558481200273653869007748824196562505786239666485471555966291112983736951766670645697366111231875196719375404918465359586631032662152618911181720172575660524484837026461986817150967447590164295940992437043173465342090347250775447874943778770250367434617242637315214277317556135938971844685657812075687228862573712892282739134214117141726476000686890324367109796601660678611548541166597572564340533765724794109280235330448590968248905521214627135168262622571194010392119103507275107979860912181344867133382763612980841393600341803100893579429950462901049274913678337894028121698461052377380025981326948586601483352432601025294046664633347628723417977076348137975007496228840829928988207220293806431806865223764142032294713777534380126248065755466537486906862158470240111674797305068861971055792401236383968550028922944926924153914829498216587968287858411869403480022273555649404406428972981810316349254813635950950411541203065941713459448875543756720453608079034933253270087932161407466518912255547896726159925353358124249128629656091304542164083162449152584811274590141262216388083116228576415159359070065336407372118480901608696953908483134181648875875742793102725125532528988968153884670241912610933074059057717754404082921550826499201045344748896058771367287858224893078254478499945619645060219463347799005744645078262000479736221453476032279629181870369976904685997941940845498810025345286575227111177643301316244964476322130419120226409611312290331355759946678820350591993256745976799361410330986390953127193612879984860265931942325862288422582668110862048305581283047377542488680226293434449550494282761735771790912389746058047493826819532573855795933290543094093195512664997842872028106905851885780992000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
*/

here thread.join(150) waits fro 150ms and if in that time the computation is done or not its ends that threads , thats why we see “is still in progress” as output.
Other codes are self explanatory.

Thread class also provides lots of function which I have not covered.Head over to following link for detailed info
https://www.geeksforgeeks.org/java-lang-thread-class-java/

Important Point regrading threads:

1) The number of threads you have depends on the number of cores in your CPU.Each CPU core can have two threads. So a processor with two cores will have four threads. A processor with eight cores will have 16 threads.
2) The CPU (central processing unit) is the core of every smartphone, tablet, and computer. It is a critical component that dictates the way your computer will perform and determines how well it can do the job.
3) Moore’s Law takes its name from Intel co-founder Gordon Moore. It is Moore’s idea that the number of transistors in an integrated circuit doubles every two years.
4) Hyper-threading technology allows a single CPU core to act as two cores, speeding up the execution of a particular program or application.