问题
I need to decide whether to use 2 threads, or 2 processes (in either case, both will connect to the same database).
2 threads would be able to share the same memory, and there's no need for them to write to that shared memory: even though they are doing different jobs, they will only read from the memory and not change it.
So my questions are:
Might I have a problem when 2 different programs insert or extract values from the database if they both run at the same time? (I know that I can synchronize my threads)
Which will have better performance? Which will be faster?
With 2 threads, it's harder to debug the program than with 2 processes. Which design will be more correct and easier for me to work with?
If i work with 2 processes, I will need to connect to the database twice; whereas threads could share the same memory.
I am using Java and MySQL.
回答1:
1) Mysql use a lock system. Depending on your engine, the lock will be on an entire table (MyISAM) or on just one line (InnoDB). Basically 2 program can access the same database without problem. source
MySQL uses row-level locking for InnoDB tables to support simultaneous write access by multiple sessions, making them suitable for multi-user, highly concurrent, and OLTP applications. MySQL uses table-level locking for MyISAM, MEMORY, and MERGE tables, allowing only one session to update those tables at a time, making them more suitable for read-only, read-mostly, or single-user applications.
2) The performance's difference between two thread and two process depends on your implementation Usually you use two thread because you want execute multiple task at the same time but each task will depends on the others. For example if you want to calculate something for every value between 0 and 100, you can create two thread. The first will calculate all value for 0...50 and the second for 51...100. Thread have better performance in this case.
On the contrary, you use two process when each process is independent and doesn't require the other to work properly. For example, you can have a process which calculate some data for n between 0 and 100 and another who display the result. The calculation process doesn't recquire the display process to work. The display process will just lock where the calculation result's are stored and display them. Using a thread in this case doesn't improve performance and can be slower.
If you want an example closer to reality, if you go shopping with a friend at a supermarket, you can split your list in two and go take the product on your part of the list when your friend is taking care of the other part. At the end, you'll meet again to pay for the entire list. This is similar to multiple thread.
On the contrary, if you want to go rent a film (old time's) and your friend is cooking, you don't need each other to accomplish your task. This is multiple process.
3) If you want to use multiple thread to work simultanously, I suggest to have one master thread that gives order to other threads. In the example of the grocery store, you have one thread to split the list and two thread to take what's on the list. At the end, the "master thread" will take everything and pay.
4) If you want to use thread only because of the open SQL connection it's not worth it and I strongly recommand to have 1 connection per thread.
Moreover, connection are pooled. When you open a new connection it doesn't really open a new connection, it use an existing one currently unused.
So having one connection open in order to use it between two thread is not worth it and could lead even to wierd bug if your driver implementation of JDBC is not thread safe.
回答2:
If these two computational units have no interaction with each other and have no any shared data (except you database) then technically there's no significant difference between implementation of this as threads or processes. You can implement these as you feel comfortable, and if you are using some application server I'd separate solution to two projects, maybe deriving common codebase as separate subproject. But this depends on the nature of you application - whether it would be logically appropriate to keep it unified or split into separate apps.
回答3:
what you need is "Transaction Isolation Level" this can help you http://dev.mysql.com/doc/refman/5.6/en/dynindex-isolevel.html
回答4:
Regardless of whether you use threads or processes for Java, each should have its own connection to MySQL. Use InnoDB and transactions and let the MySQL server keep things straight between the two connections.
来源:https://stackoverflow.com/questions/28938193/2-threads-vs-2-processes-design-performance-isolation