Multithreaded---have four threads 1, 2, 3, 4. The function of thread 1 is output 1, the function of thread 2 is output 2, etc... There are now four file ABCD

Source: Internet
Author: User

There are four threads of 1, 2, 3, 4.
The function of thread 1 is output 1,
The function of thread 2 is output 2, etc... There are now four file ABCD.
The initial is empty.
Now you want four files to appear in the following format:
A:1 2 3 4 1 2 ....
B:2 3 4 1 2 3 ....
C:3 4 1 2 3 4 ....
D:4 1 2 3 4 1 ....
Please design the program.

Try writing out four threads to write an a file alternately.

 Public  class fourthreadonefile {     Public StaticFile File =NewFILE (); Public Static void Main(string[] args) {System.out.println ("Hello world!"); Executorservice executors = Executors.newfixedthreadpool (5); Executors.execute (NewPrinttask (1)); Executors.execute (NewPrinttask (2)); Executors.execute (NewPrinttask (3)); Executors.execute (NewPrinttask (4));    Executors.shutdown (); }StaticClass Printtask implements Runnable {Private intID =0; Public Printtask(intID) { This. id = ID; } Public void Run(){ while(true) {file.print (ID); }}}}class file{//private Static Lock = new Reentrantlock ();    Private Static intState =0;Private StaticPrintWriter out; Public FILE(){Try{out =NewPrintWriter ("A.txt"); }Catch(Exception FileNotFound) {        }    } Public Static synchronized void  Print(intID) {if(state = = (ID-1)){Try{out.print (ID);                SYSTEM.OUT.PRINTLN (ID); Thread.Sleep ( +); }Catch(Interruptedexception Ex1) {            }finally{//Flush bufferOut.flush (); } state++;if(state = =4) {out.println (); State =0; }        }    }}

Expand to four files

Specify the ID and name for the task class
ID indicates the order of printing
Name indicates the characters printed when printing

The ID needs to be adjusted sequentially in the file class as appropriate.

 Public  class fourthreadfourfile {     Public StaticFile File =NewFILE (); Public Static void Main(string[] args)throwsException {System.out.println ("Hello world!"); PrintWriter o =NewPrintWriter ("B"); O.println ("Hello");        O.close (); Executorservice executors = Executors.newfixedthreadpool (5); Executors.execute (NewPrinttask (1,"1")); Executors.execute (NewPrinttask (2,"2")); Executors.execute (NewPrinttask (3,"3")); Executors.execute (NewPrinttask (4,"4"));    Executors.shutdown (); } Public Static  class printtask implements Runnable {        Private intID =0; PublicString name; Public Printtask(intId,string name) { This. id = ID; This. name = name; } Public void Run(){ while(true) {File.printfile (ID, This); }}}}class file{//private Static Lock = new Reentrantlock ();    //Represents the number of printing required    Private Static intState =0;//Select Action file    //0---A    //1---B    //2---C    //3---D    Private Static intselect =0;Private StaticPrintWriter OutA;Private StaticPrintWriter OutB;Private StaticPrintWriter OUTC;Private StaticPrintWriter Outd;Private StaticPrintWriter out; Public FILE(){Try{OutA =NewPrintWriter ("A.txt"); OutB =NewPrintWriter ("B.txt"); OUTC =NewPrintWriter ("C.txt"); OUTD =NewPrintWriter ("D.txt"); }Catch(Exception FileNotFound) {        }    } Public Static synchronized void Printfile(intId,fourthreadfourfile.printtask PT) {Switch(select) { Case 0: out = OutA; Print (ID,PT); Break; Case 1: out = OutB;//Adjustment IDid = ID-1;if(ID <=0) {id + =4; } print (ID,PT); Break; Case 2: out = OUTC;//Adjustment IDid = ID-2;if(ID <=0) {id + =4; } print (ID,PT); Break; Case 3: out = OUTD; id = ID-3;//Adjustment ID                if(ID <=0) {id + =4; } print (ID,PT); Break; }    } Public Static synchronized void  Print(intId,fourthreadfourfile.printtask PT) {if(state = = (ID-1)){Try{Out.print (pt.name); System.out.println ((Char)(' A '+select) +"-----"+ Pt.name); Thread.Sleep ( +); }Catch(Interruptedexception Ex1) {            }finally{//Flush bufferOut.flush (); } state++;if(state = =4) {out.println (); State =0; select++;if(select = =4) {select =0; }            }        }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The

Multithreaded---has four threads 1, 2, 3, 4. The function of thread 1 is output 1, the function of thread 2 is output 2, etc... There are now four file ABCD

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.