Detailed description of simultaneous startup or execution of Java Multithreading

Source: Internet
Author: User

For example, in a racing game, there are 10 cars in it, and I need them to work at the same time. This is also very important and requires high accuracy, because it is fair only when every car can start at the same time.

Each vehicle uses a thread. How can these 10 threads ensure the accuracy of "starting, executing, and waking up at the same time?

Please provide implementation or ideas, especially javaeyer for game development...
Question added:
Add:

Obviously, the following methods do not work:

Java code
New CarThread (). start ();
New CarThread (). start ();
New CarThread (). start ();
...
...
New CarThread (). start ();
New CarThread (). start ();
New CarThread (). start ();
...
...

Accuracy cannot be guaranteed if there is a sequential order.

---------- I want to separate ----------

Aninfeel answered the same question:

Aninfeel wrote
All car threads start and use wait (), and then use policyall to start together.

Java code
Class CarThread {
Public void run ()
{
// Everyone is here.
Synchronized (waitObj ){
WaitObj. wait ();
}
// Start the competition
..........
}
}
// Run together
WaitObj. policyall ();
Class CarThread {
Public void run ()
{
// Everyone is here.
Synchronized (waitObj ){
WaitObj. wait ();
}
// Start the competition
..........
}
}
 
// Run together
WaitObj. policyall ();

The reason is that the javadoc of Object. policyall () is clear.

Written by javadoc

The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

Answer
2008-12-22 fjlyxx (intermediate programmer)
In fact, you don't need to let the thread start together, unless you have so many CPUs theoretically, it won't work.

What you care about is time, which depends on time. You can set a start time and an end time when the thread starts. Of course, you must be able to cheat players in the display mode.

--------------------------------------------------------------------------------
The comments of the questioner on the answer:
You can only give one sentence. I closed my eyes and clicked "adopt as the answer". I clicked it several times.

All the answers are very well...

I also figured out why it takes three seconds to start a racing game ..

Other answers
All car threads start and use wait (), and then use policyall to start together.

Java code
Class CarThread {
Public void run ()
{
// Everyone is here.
Synchronized (waitObj ){
WaitObj. wait ();
}
// Start the competition
..........
}
}
// Run together
WaitObj. policyall ();
You can only control the running time allocated by the CPU of 10 threads.

10 thread time classes. The specified number of milliseconds is a unit ~

For example, it is 100 milliseconds.

In 100 milliseconds, each thread should be allocated a maximum of 10 seconds of running time.

In 100 Hao seconds, some threads took most of the CUP time. So you can write a control class
Each thread must be executed to 10 milliseconds within 100 milliseconds. To ensure fairness,
The next round starts with another 100 ms.

If you want high accuracy, you can change the current 100 ms time unit to a smaller time unit.

The answer upstairs is correct, but there is nothing accurate. Unless you have 32 CPU Systems, haha

The landlord should first study java's multithreading, and it will be much easier to discuss it later. Let me explain to the living in the tomb.

First of all, technically, it cannot be started at the same time, unless you have multiple CPUs.
On most computers with only one cpu, only one thread can run at the same time. You can use a flexible method to achieve the effect that is similar to your requirements.

Example:
You have 10 racing cars. This racing car lasts for 10 minutes. However, only one cpu is similar to only one track. What should we do? Each racing car can run for 10 minutes. Then change the car.

To make it smoother, you can run each car for one minute and then change the car.

The explanation of the living in the tomb is this.

However, this requirement is difficult to implement in specific java programming, because the specific implementation of java Thread Scheduling depends on the operating system. I suggest you change your mind to calculate the location of each vehicle every minute. This approach should be more practical.

"It is unrealistic to say that every vehicle can start at the same time to be fair. In fact, the so-called concurrent thread execution is also divided into a small time slice for each thread to execute sequentially.
In another way, if there is a way to start all threads at the same time, it is determined by the cpu who executes the thread first after it starts. Therefore, the so-called fairness should be: the start interval of each vehicle is as small as possible, and the start sequence is random.

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.