简体   繁体   中英

communication between threads

I'm making a little java game in which I would have two threads (well as the FIRST step towards multithreading...), one for the logic and one for the drawing.

So my question is: How can I make those two communicating which each other?

Requirements:

  • accessing variables and object from a another thread

  • syncing them so they each complete a same number of "loops" in the same time. (the logic calculates and then the another one draws the results and the loop begins again...)

So how is this achievable in java?

Thanks in advance!

Methods are methods, within a thread or not. Just create an object that is visible to all of your Threads, and they'll both be able to access it.

1. Create a Class with logic and drawing methods. 
   Whose object is accessible by both the threads.

2. Now please do synchronize the atomic statements or methods.

3. So its like an object is shared between 2 threads.

One easy structure to use to communicate between threads is the BlockingQueue .

I often find if you use a BlockingQueue it will focus you on making the threads work together correctly. For example, they will not provide the facilities you are asking for because actually those facilities are not what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM