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