Write 2 threads, one print 1-52, one print a-Z, print order is 12a34b ... (Two synchronous methods using synchronous code block and synchronous method)

Source: Internet
Author: User

1. Synchronization method

Package synchronized;/************************************ Synchronization Method ****************************************/public Class Printtest {public static void main (string[] args) {print p = new Print (); Thread T1 = new Printnumber (p); Thread t2 = new Printword (p); T1.start (); T2.start ();}} Class Printnumber extends Thread {//print digital thread private print p;public printnumber (print p) {THIS.P = P;} public void Run () {for (int i = 0; i <; i++) {P.printnumber ();}}} Class Printword extends Thread {//print letter thread private print p;public Printword (print p) {THIS.P = P;} public void Run () {for (int i = 0; i <; i++) {P.printword ();}}} Class Print {//sync monitor is print class private int i = 1;private char j = ' A ';p ublic Print () {}public synchronized void P      Rintnumber () {//Sync method System.out.print (string.valueof (i) + string.valueof (i + 1)); i + = 2;notifyall (); Wake up other processes first, then block this process, and if the order is reversed, the process can no longer wake up other process try {wait ();} catch (Interruptedexception e) {e.printstacktrace ()}} Public synchronized void Printword () {System.out.print (j); j++;notifyall (); Try{if (J <= ' Z ')//output Z no more waiting. {Wait ();}} catch (Interruptedexception e) {e.printstacktrace ();}}}

2. Synchronizing the code block package Threaddemo;

/** * < Write two threads, one thread prints 1-52, another thread prints letters A-Z. Print order is 12a34b56c ... 5152z> * * * *
/***************************************** Synchronous code block *********************************************/public class threaddemo{//test public static void main (string[] args) throws Exception {Object obj = new Object (); Start two threads Thread1 t1 = new Thread1 (obj); Thread2 t2 = new Thread2 (obj); T1.start (); T2.start (); }}//a thread prints 1-52class Thread1 extends thread{private Object obj; Public Thread1 (Object obj) {this.obj = obj; public void Run () {synchronized (obj) {//print 1-52 for (int i = 1; i < 53; i++) {System.out.print (i + ""); if (i% 2 = = 0) {//cannot forget to wake up other threads Obj.notifyall (); try {obj.wait (); } catch (Interruptedexception e) { E.printstacktrace (); }}}}}}//another thread printing letters A-zclass Thread2 extends thread{private Object o bj Public Thread2 (Object obj) {this.obj = obj; The public void Run () {synchronized (obj)///synchronization monitor is the Obj class, and the synchronization code block is written in the Run method. {//print A-Z for (int i = 0; i <; i++) {System.out.print (char) (' A ' + i) + ""); Do not forget to wake up other threads Obj.notifyall (); try {///The last one Don't wait if (i! = 25) { Obj.wait (); }} catch (Interruptedexception e) {e.printstacktrace (); } } } } }

Write 2 threads, one print 1-52, one print a-Z, print order is 12a34b ... (Two synchronous methods using synchronous code block and synchronous method)

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.