Java multithreading synchronization mechanism

Source: Internet
Author: User

The multithreading synchronization mechanism for Java is the same as in other languages, where, in the current thread, you wait for another thread to run the end or another thread's event, and then decide what to do with the event.

This example comes from a book instance, streamlines the code, and adjusts some of the logic to make it look more concise and straightforward. has run through.

The code is as follows:

Package Swingexample;import Java.awt.borderlayout;import Java.util.random;import javax.swing.jframe;import Javax.swing.jprogressbar;public class Multiprogress extends JFrame {/** * */private static final long Serialversionuid = 1l;private thread Threada = null;private thread threadb = null;final JProgressBar Progressa = new JProgressBar (); final JPr Ogressbar PROGRESSB = new JProgressBar (); int count = 0;public static void Main (string[] args) {//TODO auto-generated Meth OD stubinit (New multiprogress (), 640, 480);} Public multiprogress () {super (); Getcontentpane (). Add (Progressa, Borderlayout.north);//Put it on the northernmost side of the window Getcontentpane (). Add (PROGRESSB, Borderlayout.south);//placed at the South end of the window progressa.setstringpainted (true);p rogressb.setstringpainted (true); Initialize Threadthreada = new Thread (new Runnable () {int nprogresspercenta = 0;public void Run () by using an internal anonymous class () {//To achieve the desired effect, this random number is required must be greater than 0, and can be randomly generated, so the effect of multi-thread synchronization is more apparent to the random rand = New Random (), int n = rand.nextint (+), while (n = = 0) n = rand.nextint; whil E (NprogresspeRcenta <=) {progressa.setvalue (nprogresspercenta++); try {thread.sleep (+); if (Nprogresspercenta >= N) {// If the current progress bar has exceeded this random threshold, stop to wait for another thread to Threadb.join ();}} catch (Exception e) {e.printstacktrace ();}}}); Threada.start (); threadb = new Thread (new Runnable () {int nprogresspercent = 0;public void Run () {while (Nprogresspercent & lt;= {progressb.setvalue (nprogresspercent++); try {thread.sleep (+);} catch (Exception e) {e.printstacktrace ();}}}); Threadb.start ();} public static void Init (JFrame jf, int width, int height) {jf.setdefaultcloseoperation (jframe.exit_on_close); jf.setsize (width, height); jf.setvisible (True);}}
The results of the operation are as follows:




Java multithreading synchronization mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.