Thread and Java swing
Java swing non-thread security
Special rules must be observed to access swing objects in the case of multithreading (ensure that only swing objects are accessed from a single thread ).
The swing program has multiple threads. The event dispatching thread is responsible for accessing all swing objects and modifying the object status, keeping all access to swing objects sent by event dispatching threads is an important concern for troubleshooting multithreading.
Exceptions and rules
- Unrendered swing objects can be created and operated by any thread. However, when a swing object has been rendered, it can only be accessed by the event dispatching thread.
- The repaint method of the swing component can be called from any thread.
- The invokelater () method can be called from any thread.
- The invokeandwait method can be called from any thread that is not an event.
Example _ simulate time-consuming tasks
Package Org. ybygjy. thread3th. demo2; import Java. AWT. gridlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. util. hashmap; import Java. util. map; import Java. util. observable; import Java. util. observer; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jpanel; import javax. swing. jtextfield; import javax. swing. swingutilities; Import javax. swing. border. titledborder; /*** thread and swing * @ author wangyancheng * @ version 2012-10-31 */public class swingthread extends jframe implements observer {/*** serial ID */Private Static final long serialversionuid = random; /** Global Object */private Map <string, jtextfield> context;/** enter the Class component ID prefix */Private Static string input_field_prefix = "INS _"; /** output class component ID prefix */private stat IC string output_field_prefix = "ous _";/*** constructor */Public swingthread () {context = new hashmap <string, jtextfield> (); setdefaclocloseoperation (jframe. exit_on_close); jpanel ctxpanel = new jpanel (); ctxpanel. setborder (New titledborder ("calculate the number of Fibonacci:"); system. out. println (getcontentpane (). getlayout (); gridlayout glinst = new gridlayout (10, 1); ctxpanel. setlayout (glinst); getcontentpane (). add (Ctxpanel); createtask (ctxpanel); Pack ();}/*** Create task ** @ Param mainpanel main panel */private void createtask (jpanel mainpanel) {for (INT I = 1; I <= 10; I ++) {jlabel tmpljlin = new jlabel ("input:"); jlabel tmpljlou = new jlabel ("output: "); jpanel JP = new jpanel (); JP. setborder (New titledborder ("th ". concat (string. valueof (I )). concat ("group"); JP. add (tmpljlin); jtextfield JTF = new jtextfield (5); final string I Nputkey = input_field_prefix.concat (string. valueof (I); reginscomp (inputkey, JTF); JP. add (JTF); JP. add (tmpljlou); jtextfield jtfo = new jtextfield (5); final string outputkey = output_field_prefix.concat (string. valueof (I); reginscomp (outputkey, jtfo); JP. add (jtfo); jbutton jtnbtn = new jbutton ("computing"); jtnbtn. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {fibbe An fbinst = new fiber Bean (); fbinst. setinputkey (inputkey); fbinst. setoutputkey (outputkey); fbinst. setfibterm (getinsvalue (inputkey); new maid (swingthread. this, fbinst) ;}}); JP. add (jtnbtn); mainpanel. add (JP) ;}}/*** register component * @ Param key * @ Param jtf obj */private void reginscomp (string key, jtextfield JTF) {context. put (Key, JTF);}/*** get component value * @ Param key * @ return rtnv 0 by default */P Rivate long getinsvalue (string key) {return context. containskey (key )? Integer. parseint (context. get (key ). gettext (): 0;}/*** test entry * @ Param ARGs parameter list */public static void main (string [] ARGs) {swingutilities. invokelater (New runnable () {public void run () {New swingthread (). setvisible (true) ;}}) ;}/ *** {@ inheritdoc} */Public void Update (observable o, object Arg) {If (ARG instanceof fiber bean) {final maid = (maid) ARG; swingutilities. invokelater (New runnable () {public void run () {context. get (fig. getoutputkey ()). settext (string. valueof (fig. getfibvalue ()));}});}}}
Package Org. ybygjy. thread3th. demo2; import Java. util. observable; import Java. util. observer;/*** fibonaccitask * @ author wangyancheng * @ version 2012-11-1 */public class fibonaccitask extends observable implements runnable {/** {@ link fiber bean} */private fiber bean; /*** constructor * @ Param observer {@ link observer} * @ Param fabbean fibbean */Public fibonaccitask (Observer observer, fiber bean fibbean) {This. maid = maid; addobserver (observer); New thread (this ). start ();}/*** {@ inheritdoc} */Public void run () {This. fiber bean. setfibvalue (calc (this. fiber bean. getfibterm (); setchanged (); policyobservers (this. fibbean);}/*** computing logic (recursion) * @ Param fibnum number of items * @ return rtnv */private long calc (long fibnum) {If (fibnum <= 2) {return 1;} return (calc (fiber num-1) + calc (fiber num-2 ));}}
Package Org. ybygjy. thread3th. demo2;/*** data Bean * @ author wangyancheng * @ version 2012-11-1 */public class fibbean {/** input object key */private string inputkey; /** output object key */private string outputkey;/** Fibonacci item */private long fiber term;/** Fibonacci value */private long fiber value; /*** @ return the inputkey */Public String getinputkey () {return inputkey;}/*** @ Param inputkey the inputkey to set */Public void setinputkey (string inputkey) {This. inputkey = inputkey;}/*** @ return the outputkey */Public String getoutputkey () {return outputkey ;} /*** @ Param outputkey the outputkey to set */Public void setoutputkey (string outputkey) {This. outputkey = outputkey;}/*** @ return the fiber term */public long getfiber term () {return fiber term ;} /*** @ Param fiber term the fiber term to set */Public void setfiber term (long fiber term) {This. fibterm = fibterm;}/*** @ return the fibvalue */public long getfibvalue () {return fibvalue ;} /*** @ Param fiber value the fiber value to set */Public void setfiber value (long fiber value) {This. fiber value = fiber value ;}}