2线程和2进程(设计/性能/隔离) [英] 2 Threads vs 2 Processes (design/performance/isolation)

查看:146
本文介绍了2线程和2进程(设计/性能/隔离)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要决定是使用2个线程还是使用2个进程(在任一情况下,两者都将连接到同一个数据库)。



共享相同的内存,并且没有必要写入共享内存:即使他们正在做不同的工作,他们只会从内存中读取而不改变它。



所以我的问题是:


  1. 我有一个问题,当2个不同的程序插入或从数据库中提取值如果他们都同时运行? (我知道我可以同步我的线程)


  2. 哪种会有更好的性能?


  3. 使用2个线程,调试程序比使用2个进程更难。


如果我使用两个进程,我会需要连接到数据库两次;

解决方案



我使用的是Java和MySQL。 div>

1)Mysql使用锁系统。根据你的引擎,锁定将在整个表(MyISAM)或只在一行(InnoDB)。基本上2个程序可以访问同一个数据库没有问题。 来源


MySQL对InnoDB表使用行级锁定,以支持多个会话的同时写入访问,使其适用于多用户,高并发和OLTP应用程序。 MySQL对MyISAM,MEMORY和MERGE表使用表级锁定,只允许一个会话一次更新这些表,使它们更适合于只读,读取主要或单用户应用程序。


2)两个线程和两个进程之间的性能差异取决于你的实现通常你使用两个线程,因为你要执行多个任务同一时间,但每个任务将取决于其他。例如,如果你想为0和100之间的每个值计算一些东西,你可以创建两个线程。第一个将计算0 ... 50的所有值,第二个计算51 ... 100。在这种情况下,线程具有更好的性能。
$
相反,当每个进程是独立的,并且不需要其他进程正常工作时,您使用两个进程。例如,您可以有一个过程,计算0和100之间的n的一些数据,以及显示结果的另一个数据。计算过程不要求显示过程工作。显示过程将只锁定存储计算结果的位置并显示它们。在这种情况下使用主题不会提高效果,而且可能会更慢。


如果您想要更接近现实的例子,如果您与朋友一起去购物超市,你可以把你的列表分为两部分,当你的朋友照顾其他部分时,把产品放在你的名单上。最后,你会再次见面,支付整个列表。这与多线程类似。

\\
相反,如果你想租电影(旧时间),而你的朋友正在做饭,你不需要彼此完成你的任务。这是多个过程。



3)如果你想使用多线程同时工作,我建议有一个主线程给其他线程。在杂货店的示例中,您有一个线程来拆分列表,两个线程要采取列表中的内容。最后,主线程将采取一切和支付。



4)如果你想使用线程只是因为开放的SQL连接,这是不值得的,我强烈建议每个线程有1个连接。

此外,连接已汇集。当您打开一个新的连接,它不会真正打开一个新的连接,它使用现有的一个目前未使用。
因此有一个连接打开,以便在两个线程之间使用它是不值得的,可以导致甚至wierd错误,如果你的驱动程序实现JDBC不是线程安全的。


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:

  1. 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)

  2. Which will have better performance? Which will be faster?

  3. 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) 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线程和2进程(设计/性能/隔离)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆