Regarding the examination registration system, randomly takes the ticket number question

Source: Internet
Author: User
Tags return tag
/**
*
*/
Package com.test.testSynchronized;


Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Map.Entry;


/**
*
* Analog Fetch number
* @author Chenshuai
* @ August 18, 2016
* @ 11:37:54
*/
public class Testsynchronized {


Static Map<integer, integer> Map = new Hashmap<integer, integer> ();

Public synchronized Integer Getnumsynmethod () {
int size = Map.size ();
int index = 1+ (int) (Math.random () *size);
Integer tag = map.get (index);
Map.Remove (index);
return tag;
}

public static void Main (string[] args) {

Map.put (1, 1);
Map.put (2, 2);
Map.put (3, 3);
Map.put (4, 4);
Map.put (5, 5);
Map.put (6, 6);
Map.put (7, 7);
Map.put (8, 8);
Map.put (9, 9);

testsynchronized T1 = new testsynchronized ();
testsynchronized t2 = new testsynchronized ();
testsynchronized t3 = new testsynchronized ();

Iterator<entry<integer, integer>> it = Map.entryset (). iterator ();
while (It.hasnext ()) {
Entry<integer, integer> Entry = It.next ();
System.out.println ("Original value is:" +entry.getvalue ());
}

Integer i = T1.getnumsynmethod ();
SYSTEM.OUT.PRINTLN ("Fetch:" +i);

Iterator<entry<integer, integer>> it2 = Map.entryset (). iterator ();
while (It2.hasnext ()) {
Entry<integer, integer> Entry = It2.next ();
System.out.println ("The + i +" is taken, the map value is: "+entry.getvalue ());
}

Integer i2 = T2.getnumsynmethod ();
SYSTEM.OUT.PRINTLN ("Fetch:" +I2);

Iterator<entry<integer, integer>> it3 = Map.entryset (). iterator ();
while (It3.hasnext ()) {
Entry<integer, integer> Entry = It3.next ();
System.out.println ("after" + I2 +), the map value is: "+entry.getvalue ());
}

Integer i3 = T3.getnumsynmethod ();
SYSTEM.OUT.PRINTLN ("Fetch:" +I3);

Iterator<entry<integer, integer>> it4 = Map.entryset (). iterator ();
while (It4.hasnext ()) {
Entry<integer, integer> Entry = It4.next ();
System.out.println ("after" + i3 +), the map value is: "+entry.getvalue ());
}

if (It.hasnext ()) {
Entry<integer, integer> Entry = It.next ();
System.out.println ("Finished value is:" +entry.getvalue ());
}
}

}


Run results



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.