A Java multi-threaded face question

Source: Internet
Author: User

Thread A prints the digital 0--12;

Thread B prints the letter a--z;

Print Result: 0ab1cd2ef3gh4ij5kl6mn7op8qr9st10uv11wx12yz

Required to use the line threads transmission value;

Analysis: Thread A prints a number, thread B prints two letters, makes 13 loops,

Threads the value of a line through a public resource class

public class Fftthreadtest {
public static void Main (string[] args) {//Create final Resource object final business business = new Business (); Char c= ' a ';
for (int i=0;i<=12;i++) {final char a= c;business.pringtint (i); business.setc (a); new Thread (New Runnable () {@ overridepublic void Run () {business.pringtstring ();}}). Start (); c= (char) ((int) c+2);}}} /**
Business class: 1 printing numbers, 2 printing letters,
**/class Business {Private Boolean isint=true;//Mark whether to print the number private char C;//the letter to be printed
Print Digital Sync lock public synchronized void pringtint (int i) {
If the number is not waiting while (!isint) {try {wait ();} catch (Interruptedexception e) {e.printstacktrace ()}} System.out.print (i);
Modify Mark Isint=false;this.notify (); Wake-Up Wait thread}
Print Letter Sync lock public synchronized void pringtstring () {
If the number is waiting while (Isint) {try {wait ();} catch (Interruptedexception e) {e.printstacktrace ()}}
Print two letters for (int i=0;i<2;i++) {System.out.print (c); C + +;}
Modify Mark Isint=true;this.notify (); Wake-up wait thread}public void setc (char c) {this.c=c; }}

  

A Java multi-threaded face question

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.