You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -2215,8 +2213,8 @@ public class SimpleChatClient extends JFrame {
2215
2213
// 了一个新线程。该线程的作业,是从服务器的套接字流进行读取,之后
2216
2214
// 在滚动文本区显示出全部传入消息。
2217
2215
//
2218
-
// We're starting a new thread, using a new inner class as the
2219
-
// Runnable(job) for the thread. The thread's job is to read
2216
+
// We're starting a new thread, using a new inner class as the
2217
+
// Runnable(job) for the thread. The thread's job is to read
2220
2218
// from the server's socket stream, displaying any incoming
2221
2219
// messages in the scrolling text area.
2222
2220
Thread readerThread = new Thread(new IncomingReader());
@@ -2351,7 +2349,7 @@ public class VerySimpleChatServer {
2351
2349
} catch (Exception ex) {ex.printStackTrace();}
2352
2350
}
2353
2351
}
2354
-
2352
+
2355
2353
publicvoidtellEveryOne(Stringmessage) {
2356
2354
Iterator it = clientOutputStreams.iterator();
2357
2355
@@ -2386,7 +2384,7 @@ public class VerySimpleChatServer {
2386
2384
> 事实上,这里就 *应该* 把这些方法同步化,以阻止其他线程以别的方式来访问这些方法。由于这里的示例并没有其他代码会读写到银行账户,因此就没有那样做(Actually, we *should* have synchronized those methods, to prevent other threads from accessing those methods in other ways. We didn't bother, because our example didn't have any other code accessing the account)。
2387
2385
>
2388
2386
> 不过需要补充的是,对读取器与设置器(或者说这个示例中的 `checkBalance()` 与 `withdraw()`)的同步化,是不充分的。请记住,同步化的点位,是要令到某个特定代码部分以 **原子方式** 工作。也就是说,同步化不光是所要关注的一些单个方法,而是那些要求 **多个步骤去完成** 的方法!请稍加思考。假如这里没有将 `makeWithdrawl()`方法同步化,Ryan 就会检查完账户余额(通过调用同步化的 `checkBalance()` 方法),然后立即退出该方法并交回那把钥匙(But synchronizing the getters and setters(or in this case the `checkBalance()` and `withdraw()`) isn't enough. Remember, the point of sysnchronization is to make a specific section of code work ATOMICALLY. In other words, it's not just the individual methods we care about, it's methods that requires ***more than one step to complete***! Think about it. If we had not synchronized the `makeWithdrawal()` method, Ryan would have checked the balance(by calling the synchronized `checkBalance()`), and then immediately exited the method and returned the key)!
2389
-
>
2387
+
>
2390
2388
> 当然他就在他醒来后,再度取得那把钥匙,这样他就可以调用那个同步的 `withdraw()`方法,然而这样做仍留下了在同步化之前曾遇到过的同样问题!Ryan能够检查余额、睡过去,然后 Monica 就可以进入并在 Ryan 有机会醒来且完成他的提取之前,对余额进行检查(Of course he would grad the key again, after he wakes up, so that he can call the synchronized `withdraw()` method, but this still leaves us with the same problem we had before synchronization! Ryan can check the balance, go to sleep, and Monica can come in and also check the balance before Ryan has a chance to wakes up and completes his withdrawal)。
2391
2389
>
2392
2390
>因此将全部存取方法进行同步化,从而阻止其他线程介入,可能是个不错的主意,但仍需把那些具有必须以一个原子单元方式执行语句的方法,加以同步化(So synchronizing all the access methods is probably a good idea, to prevent other threads from getting in, but you still need to synchronizing the methods that have statements that must execute as one atomic unit)。
@@ -2442,20 +2440,20 @@ public class BeatBoxFinal extends JFrame{
2442
2440
JLabel tempoLabel =null;
2443
2441
2444
2442
String [] instrumentNames = {
2445
-
"贝斯鼓(低音鼓)",
2443
+
"贝斯鼓(低音鼓)",
2446
2444
"闭镲(闭合击镲)",
2447
-
"空心钹(开音踩钹)",
2448
-
"小鼓(军鼓)",
2449
-
"双面钹(强音钹)",
2445
+
"空心钹(开音踩钹)",
2446
+
"小鼓(军鼓)",
2447
+
"双面钹(强音钹)",
2450
2448
"拍手(拍掌声)",
2451
-
"高音鼓(高音桶鼓)",
2452
-
"高音圆鼓(高音小鼓)",
2453
-
"沙锤(沙铃)",
2454
-
"口哨",
2449
+
"高音鼓(高音桶鼓)",
2450
+
"高音圆鼓(高音小鼓)",
2451
+
"沙锤(沙铃)",
2452
+
"口哨",
2455
2453
"低音手鼓",
2456
-
"牛铃(牛颈铃)",
2457
-
"颤音叉",
2458
-
"中低音桶鼓",
2454
+
"牛铃(牛颈铃)",
2455
+
"颤音叉",
2456
+
"中低音桶鼓",
2459
2457
"高音撞铃",
2460
2458
"开音高音手鼓"};
2461
2459
@@ -2534,7 +2532,7 @@ public class BeatBoxFinal extends JFrame{
0 commit comments