java - How to use and create a daemon thread and optimal utilization with Swing -
i creating blackjack game high school programming class , need method tracks card values , bets run in background. research on website revealed daemon threads answer. if not true, please provide me suitable alternative , include examples of how use , create alternative.
my problem not understand how create daemon thread. appreciate explanation , example of how use , create daemon thread.
there 1 more problem, program, created resource class contains code dealer. players subclasses of dealer , subclasses of jpanel. in subject, blackjack game uses swing display game , "optimal utilization" comes in. have application file in dealer , player objects entered so:
public class panelproject extends jpanel { private playerboxclubs playerone; private playerboxhearts playertwo; private playerboxspades playerthree; private dealerboxdiamonds dealerone; public panelproject() setlayout(new borderlayout()); jpanel centersub = new jpanel(); centersub.setlayout(new flowlayout()); add(centersub, borderlayout.south); playerone = new playerboxclubs(); add(playerone, borderlayout.west); playertwo = new playerboxhearts(); add(playertwo, borderlayout.center); playerthree = new playerboxspades(); add(playerthree, borderlayout.east); dealerone = new dealerboxdiamonds(); add(dealerone, borderlayout.north); jbutton quit = new jbutton("quit"); quit.addactionlistener(new quitlistener()); centersub.add(quit, borderlayout.south); }
so, second question this: should place daemon thread, in application file, in dealer class (which superclass players) or somewhere else?
thread t = new thread(new runnable() { public void run() { // interesting background tasks, in while(running) loop. } }); // next line answer question t.setdaemon(true); t.start();
the difference between regular thread , daemon thread jvm happily exit if threads running marked daemon.
answering "where place it" harder, depends on structure have, , how work you're going in run() method. broadly, more work does, more should in own file/class (the 2 more or less same thing in java).
Comments
Post a Comment