Home > Back-end >  Multi-threaded to sell tickets to the theatre, how to play 1000 tickets are sold and statistical tim
Multi-threaded to sell tickets to the theatre, how to play 1000 tickets are sold and statistical tim

Time:11-29



Want to have a look at what's going on
,
1000 tickets, for example, one thread to sell it to 30 ms

The open three threads should as long as 10 ms can sell out,

That how to statistics the time-consuming,,



CodePudding user response:

 
For (Thread Thread: threads) {
Thread. The join ();
}

CodePudding user response:

How to do ah, not to say that to join the thread will become a thread, that such a multithreaded changes has been single, have no meaning

CodePudding user response:

thread

CodePudding user response:

Using Thread. The activeCount ();

CodePudding user response:

CountDownLatch meets your needs

CodePudding user response:

refer to fifth floor a handsome forced response:
countDownLatch meets your demand

The
reference 4 floor & amp; Low_Key response:
using Thread. The activeCount ();

Big trouble for example, the novice small white one

CodePudding user response:

Buy before read the current time, sold out in read the current time, so can you

CodePudding user response:



I do not know how to implement, multithreading don't know how to operate, it is good to have a code

CodePudding user response:

Threads are very few people to play?
Should be very sweet

CodePudding user response:

This is a countDownLatch
https://blog.csdn.net/dotnetstudio/article/details/105612394

CodePudding user response:

 package pack; 

/*
The movie tickets
*/

Public class Solution {

Private static int count=1000;

Private static Object obj=new Object ();

Public static void main (String [] args) {
Buy wuxia=new Buy ();
Buy kehuan=new Buy ();
Wuxia. Elegantly-named setName (" blind several dozen ");
Kehuan. Elegantly-named setName (" fire off toward the ");
Wuxia. Start ();
Kehuan. Start ();
}
Public static class Buy extends Thread {

@ Override
Public void the run () {
long startTime=System.currentTimeMillis();
While (true) {
Synchronized (obj) {
If (count==0) {
long endTime=System.currentTimeMillis();
Float time=(float) (endTime - startTime)/1000;
System. The out. Println (Thread. CurrentThread (). The getName () + "time consuming" + time);
break;
} else {
The count -;

}
System. Out. Println (Thread. CurrentThread (). The getName () + "sell a remaining" + count + ", ");
}

}
}
}
}


Blind several dozen sell a piece, the remaining 20,
Blind several dozen sell a piece, the remaining 19 pieces,
Blind several dozen sell a piece, the remaining 18,
Blind several dozen sell a, remaining 17,
Blind several dozen sell a, the rest of the 16 pieces,
Blind several dozen sell a, the rest of the 15 pieces,
Blind several dozen sell a piece, the remaining 14,
Blind several dozen sell a piece, the remaining 13 pieces,
Blind several dozen sell a piece, the remaining 12,
Blind several dozen sell a piece, the remaining 11 pieces,
Blind several dozen sell a piece, the remaining 10,
Blind several dozen sell a piece, the remaining 9 pieces,
Blind several dozen sell a piece, the remaining eight,
Blind several dozen sell a piece, the remaining 7,
Blind several dozen sell a, the remaining 6 pieces,
Blind several dozen sell a, the remaining five tickets,
Blind several dozen sell a piece, the remaining four,
Blind several dozen sell a, the remaining three,
Blind several dozen sell a, the remaining two,
Blind several dozen sell a, the remaining 1,
Blind several dozen sell a, remaining 0,
Blind several dozen takes 0.05
Fire off toward the time-consuming 0.065

Process is over, the exit code 0

If everyone like to watch a blind several dozen of martial arts, also not too like fire off toward the

CodePudding user response:

reference at any time the 13th floor efforts won't be late reply:

Brother, you this is the timing of the single thread,

Multithreaded how timing

CodePudding user response:

The
reference 11 floor KeepSayingNo response:
this is countDownLatch
https://blog.csdn.net/dotnetstudio/article/details/105612394

Thanks brother,,

CodePudding user response:

on the 14th floor savage beast reference response:
Quote: refer to the 13th floor efforts won't be late to reply at any time:

Brother, you this is the timing of the single thread,

Multithreaded how timing


 

Public class Ts {

Public static void main (String [] args) {
//TODO Auto - generated method stub
The test ();

}
Private static void test () {
Xc Xc=new Xc ();
Long startTime=System. CurrentTimeMillis ();
Thread the t1=new Thread (xc);
Thread t2=new Thread (xc);
The Thread t3=new Thread (xc);
T1. Elegantly-named setName (" t1 ");
T2. Elegantly-named setName (" t2 ");
T3. Elegantly-named setName (" t3 ");
t1.start();
t2.start();
T3. The start ();
While (t1) isAlive () | | t2. The isAlive () | | t3. The isAlive ()) {}
Long endTime=System. CurrentTimeMillis ();
System. Out. Println (" takes the current program, "+ (endTime - startTime)," ms ");
}
}

The class Xc implements Runnable {
Private static int count=1000;

@ Override
Public synchronized void the run () {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related