Java thread Synchronization

Source: Internet
Author: User

Business logic:

A large community, every second there are thousands of people in the submission of messages, submitted by the message will pass, tens of thousands of regular expression filtering, no match any rules, to save to the system, otherwise prompt users, you enter the content is not legal.

This is what I think, to divide the tens of thousands of regular expressions into 2000 groups, open a thread pool of 5 threads, and each thread will be responsible for matching the 2000 rules.

When each message is submitted, it will be 5 threads to determine if there is a matching rule, and if one of the threads matches the rule, it will end the task of the other 4 threads and return the results to the user.

==

Code:

Import Java.util.arraylist;import Java.util.concurrent.countdownlatch;public class Testthread {/** * @param args * @ Throws interruptedexception */public static void Main (string[] args) throws interruptedexception {String c = "Comment 1"; Txtclass tx = new Txtclass (c);        Countdownlatch Cdlatch = new Countdownlatch (5);        Thread tr = new Crthread (1,tx,cdlatch),//1 indicates the first Thread TR2 = new Crthread (2,tx,cdlatch);        Thread TR3 = new Crthread (3,tx,cdlatch);        Thread TR4 = new Crthread (4,tx,cdlatch);                Thread TR5 = new Crthread (5,tx,cdlatch);        Tr.start ();        Tr2.start ();        Tr3.start ();        Tr4.start ();        Tr5.start ();        Cdlatch.await (); System.out.println ("All done, results [" +tx.isfind () + "]");}} Class Txtclass{private string c = "";p rivate boolean isfind = False;public txtclass (string c) {this.c = C;} public Boolean isfind () {return isfind;} public void Setfind (Boolean isfind) {this.isfind = Isfind;} Public String GetC () {return c;}} Class regclass{Validation rules private static Regclass rc = new Regclass ();p ublic static Regclass getinstance () {return rc;} Private arraylist<string> list = new ArrayList ();p ublic Regclass () {//Initialization rule list.add ("S"); List.add ("s"); List.add ("S"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); List.add ("s"); ), List.add ("s"), List.add ("s"), List.add ("1"), List.add ("comment"), List.add ("a"), List.add ("B"); List.add ("R");} public boolean iscontains (int index,string c) {if (List.size () >index) {return C.indexof (List.get (index)) >=0;} Else{return false;}}} Class Crthread extends thread{private int startnum = 0;private txtclass txtclass;//message content private Countdownlatch CDLATCH;PRI vate int onelength = 2000;//The length of a thread checksum public crthread (int i,txtclass txtclass,countdownlatch cdlatch) {super (); This.startnum = I;this.txtclass = Txtclass;this.cdlatch = Cdlatch;} @Overridepublic void Run () {Boolean f = fAlse;int nums = 0;for (int i=0;i<onelength;i++) {nums = (startNum-1) *onelength+i; System.out.println ("thread-" +startnum+ "-[" +nums+ "]") f=regclass.getinstance (). Iscontains (Nums, txtclass.getc ()) ; if (f) {txtclass.setfind (true);} if (Txtclass.isfind ()) {break;} try {thread.sleep (1);} catch (Interruptedexception e) {e.printstacktrace ()}} System.out.println ("thread-" +startnum+ "-End [" +nums+ "]"); This.cdLatch.countDown ();}}


Java thread Synchronization

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.