Lecture 19: Concurrency

1 The Use of Concurrency

  • Web sites must handle multiple simultaneous users.
  • Mobile apps need to do some of their processing on servers (“in the cloud”).
  • Graphical user interfaces almost always require background work that does not interrupt the user. For example, Eclipse compiles your Java code while you’re still editing it.

2 Two Models for Concurrent Programming

2.1 Shared Memory

In the shared memory model of concurrency, concurrent modules interact by reading and writing shared objects in memory. Examples of the shared-memory model:

  • A and B might be two processors (or processor cores) in the same computer, sharing the same physical memory.
  • A and B might be two programs running on the same computer, sharing a common filesystem with files they can read and write.
  • A and B might be two threads in the same Java program (we’ll explain what a thread is below), sharing the same Java objects.

2.2 Message Passing

In the message-passing model, concurrent modules interact by sending messages to each other through a communication channel. Modules send off messages, and incoming messages to each module are queued up for handling. Examples include:

  • A and B might be two computers in a network, communicating by network connections.
  • A and B might be a web browser and a web server – A opens a connection to B and asks for a web page, and B sends the web page data back to A.
  • A and B might be an instant messaging client and server.
  • A and B might be two programs running on the same computer whose input and output have been connected by a pipe, like ls | grep typed into a command prompt.

3 Processes, Threads, Time-slicing

Process. A process is an instance of a running program that is isolated from other processes on the same machine. in particular, it has its own private section of the machine’s memory.

  • Process is natural for message passing but not natural for memory sharing(need some effort).
  • Process abstraction represents a virtual computer.

Thread. A thread is a locus of control inside a running program. Think of it as a place in the program that is being run, plus the stack of method calls that led to that place (so the thread can go back up the stack when it reaches return statements).

  • Thread abstraction represents a virtual processor.
  • Threads are automatically ready for shared memory, because threads share all the memory in the process.
  • It takes special effort to get “thread-local” memory that’s private to a single thread.
  • It’s also necessary to set up message-passing explicitly, by creating and using queue data structures

3.1 Threads in java

When you are going to create a thread in java:

  • Never use the way of subclassing Thread class.
  • Always implement the Runnable interface and use the new Thread(..) constructor.

A very common idiom is starting a thread with an anonymous Runnable:

new Thread(new Runnable() {
    public void run() {
        System.out.println("Hello from a thread!");
    }
}).start();

4 Interleaving and Race Condition

  • Interleaving: 两个线程的每一条指令按照随机的顺序交错执行。
  • A race condition means that the correctness of the program (the satisfaction of postconditions and invariants) depends on the relative timing of events in concurrent computations A and B. When this happens, we say “A is in a race with B.

5 Concurrency is Hard to Test and Debug

It’s very hard to discover race conditions using testing. And even once a test has found a bug, it may be very hard to localize it to the part of the program causing it.

  • Heisenbug is hard to trace and reproduce, println()anddebugger always does not work.
  • Bohrbug which shows up repeatedly whenever you look at it.

Reference

[1] 6.005 — Software Construction on MIT OpenCourseWare | OCW 6.005 Homepage at https://ocw.mit.edu/ans7870/6/6.005/s16/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值