1. Functional Requirements
The laboratory has a fixed number of equipment for students to experiment through the network connection, a device can only be used by one user, a user can only occupy one device.
Here is a schematic of a feature:
2. Implementation scenarios2.1 Initialization
After the project is started, the initialization of the experimental device queuing function is initiated, which is required to initialize:
A, create a new queue to store the device, and from the database to find all the normal use of the device into the queue;
b, create a new thread pool for queuing, which will be explained later;
C, create a new queue to hold the queued users.
2.2 Process implementation
Thread: The current user's request thread; Waitusers: queue for queued user threads; Exec: queued thread pool; Threada: Queued threads open in a thread pool; equipment: The thread that holds the device
3. Specific implementation 3.1. Queue initialization
1 /**2 * Initialize queue and thread pool3 * @authorYANGC4 *5 */6 Public classEquipmentqueue {7 //Device Queue8 Public StaticBlockingqueue<equipment>equipment;9 //Request QueueTen Public StaticBlockingqueue<waituser>waitusers; One //thread Pool A Public StaticExecutorservice exec; - - /** the * Initialize the device, request queue, and thread pool - */ - Public voidInitequipmentandusersqueue () { -exec =Executors.newcachedthreadpool (); +Equipment=NewLinkedblockingqueue<equipment>(); - //put the idle device in the device queue + setfreedevices (exec); Awaitusers=NewLinkedblockingqueue<waituser>(); at } - - /** - * Put idle devices in the device queue - * @paramexec - */ in Private voidsetfreedevices (Executorservice exec) { - //get the available devices toList<record> equipments=getfreeequipment (); + for(inti = 0; I < equipments.size (); i++) { -Record dc=Equipments.get (i); theEquipment de=NewEquipment (Dc.getint ("id"), dc.getstr ("Quip_no"), Dc.getstr ("name")); * Try { $ equipment.put (DE);Panax Notoginseng}Catch(interruptedexception e) { - e.printstacktrace (); the } + } A } the + /** - * Get available devices (isolate the available devices from the database) $ * @return $ */ - PublicList<record>getfreeequipment () { - returnQuipPartsManager.manager.getFreeEquipment (); the } -}
3.2. Filtering the experiment request
When the user's experiment request enters, first of all to determine whether the user in the database is not exited, if in a state that is not exited, change the status to closed, and then re-queued.
Each time the experiment is requested, a queued data is saved in the database, and information such as the queued user, status, usage time, and so on is recorded. Waituser is the class of the experiment request, the main fields are: Thread (the threading object that holds the experiment request), session (the user session for the experiment request), test (the class that corresponds to the queued data in the database). When the request is entered, the user's request object is put into the request queue in the filter.
1 //determine if the current user has an experiment that has not exited and is processed2 TestManager.manager.setUserTestInfomation (session);3 //gets the current thread4Thread thread=Thread.CurrentThread ();5 //to wait for the current user6Test test=TestManager.manager.SetUserTestStateForWait (session);7 //to create the current user request object8Waituser waituser=NewWaituser ();9 waituser.setthread (thread);Ten Waituser.setsession (session); One waituser.settest (test); A //put the current user request object into the queue - EquipmentQueue.waitUsers.add (waituser);
3.3. Execute the queued thread and suspend the current thread
A thread pool is assigned to the current request, and the thread is run, and then the request thread is suspended.
1 // assign threads to the current user request in the thread pool, run the wait to allocate the device 2 EquipmentQueue.exec.execute (waituser); 3 // pauses the current user request when whetherwait equals 2 o'clock, indicating that the device binding is complete without having to suspend the current thread 4 synchronized (thread) {5 if (Waituser.getwhetherwait ()!=2) {6 thread.wait (); 7 }8 }
Start the queue to run the experiment method in Waituser, first get a device from the device queue, if there is no device, the current thread will go into a blocking state until the queue into the device, if there is a device, remove a request object from the request queue, set the request and device binding.
1 Public classWaituserImplementsrunnable{2 //thread object for the current request3 Privatethread thread;4 //Session object for the current user5 PrivateHttpSession session;6 //used to determine whether a thread enters the wait state7 Private intWhetherwait=0;8 //user's experimental object9 Privatetest test;Ten One @Override A Public voidrun () { - //When the thread is not interrupted - while(!thread.interrupted ()) { the experiment (); - } - } - + /** - * The experimental information is stored in the database, the user information is retrieved from the session, the device used is removed from the queue, the device object is stored in session + */ A Public voidExperiment () { at Try { - //Remove a device -Equipment equipment=EquipmentQueue.equipment.take (); - EquipmentQueue.equipment.remove (equipment); -Waituser waituser=EquipmentQueue.waitUsers.take (); - EquipmentQueue.waitUsers.remove (waituser); in //bind device to user, status set to test - TestManager.manager.bindUserAndEquipment (equipment,waituser); to}Catch(interruptedexception e) { +SYSTEM.ERR.PRINTLN ("---" +e.getmessage ()); - } the } * $ Panax Notoginseng PublicTest gettest () { - returntest; the } + A Public voidsettest (test test) { the This. Test =test; + } - $ Public intgetwhetherwait () { $ returnwhetherwait; - } - the Public voidSetwhetherwait (intwhetherwait) { - This. whetherwait =whetherwait;Wuyi } the - PublicHttpSession getsession () { Wu returnsession; - } About $ Public voidSetsession (HttpSession session) { - This. session =session; - } - A PublicThread GetThread () { + returnthread; the } - $ Public voidsetthread (thread thread) { the This. Thread =thread; the } the the}Waituser
3.4. Release the request thread
When the device binding succeeds, the request thread can be freed, there is a need to be aware of the problem, suspending the request thread and releasing the request thread (to ensure that there will not be a post-release pending), put the release and the suspended thread into the code block with an explicit lock decoration, to ensure that only one is executed at the same time. When a lock is released, the value of the state whetherwait is set to 2, marking that the request is already bound to the device and does not need to be suspended.
1 Thread thread=waituser.getthread (); 2 synchronized (thread) {3 waituser.setwhetherwait (2); 4 thread.notify (); 5 }
4. Supplement 4.1. When queuing, give feedback to the client
Each request experiment will be stored in a queue of data into the database, when the completion of the device binding will be queued data status value is set to "in use", you can perform a scheduled task on the client, scheduled from the database query in "queued" request number, so that in the client in real-time display "queued, There are 10 users in front of you who are waiting for ... "effect.
4.2. After the user enters the experiment, takes a long time to take up but does not use the situation processing
When the user enters the experiment, in order to prevent the user to occupy the experiment equipment for a long time (and not in use), need to perform a timed task, every interval of time, the window to confirm whether the user is in use, if the user does not respond, it is considered that the user has left, the user and the device and exit the experiment.
4.3. User forcibly quit processing (Close page/manage browser/Shut down PC ...) The case of forced exit of the experiment)
When the user forcibly exits, the user and device are still in the bind state in the database. When the user enters the experiment, it is necessary to perform a timed task on the client to update the user's latest experiment time in real time. Then, run a timed task on the server, check the queued data in the database in real time, and determine if the current time difference between the time and the existing one is greater than the time between the client's scheduled task (considering the time of the update will have a certain delay). Data that can be properly retained), if greater than the device is unbound from the user.
Implementation of the experimental queuing function (JAVA)