@@ -33,7 +33,8 @@ Android设计模式源码解析之命令模式
33
33
## 3. 模式的简单实现
34
34
### 简单实现的介绍
35
35
命令模式其实就是对命令进行封装,将命令请求者和命令执行者的责任分离开来实现松耦合。
36
- ` 阐述一下你的示例要实现的功能 `
36
+ 这里我们通过一个简单的实例来剖析一下命令模式:命令接收者ReceiverRole拥有一个PeopleBean类型成员,通过Invoker发出不同的命令来修改PeopleBean的相对应的属性,具体命令实现类ConcreteCommandImpl1执行修改年龄命令,ConcreteCommandImpl2执行修改姓名的命令等等,ClientRole负责组装各个部分。
37
+ 例子代码如下(resource目录下也可以查看)。
37
38
38
39
### 实现源码
39
40
@@ -337,13 +338,6 @@ public class Thread implements Runnable {
337
338
Runnable target;
338
339
private static int count = 0;
339
340
340
- /**
341
- * Starts the new Thread of execution. The <code>run()</code> method of
342
- * the receiver will be called by the receiver Thread itself (and not the
343
- * Thread calling <code>start()</code>).
344
- * @throws IllegalThreadStateException if the Thread has been started before
345
- * @see Thread#run
346
- */
347
341
public synchronized void start() {
348
342
if (hasBeenStarted) {
349
343
throw new IllegalThreadStateException("Thread already started."); // TODO Externalize?
@@ -397,10 +391,6 @@ bool dvmCreateInterpThread(Object* threadObj, int reqStackSize){
397
391
398
392
}
399
393
400
- /*
401
- * Alloc and initialize a Thread struct.
402
- * Does not create any objects, just stuff on the system (malloc) heap.
403
- */
404
394
static Thread* allocThread(int interpStackSize)
405
395
{
406
396
Thread* thread;
@@ -416,17 +406,13 @@ static Thread* allocThread(int interpStackSize)
416
406
接下来在12行通过pthread_create创建pthread线程,并让这个线程start,这样就会进入该线程的thread entry运行,下来我们来看新线程的thread entry方法 interpThreadStart,同样只列出关键的地方:
417
407
418
408
```
419
- /*
420
- * pthread entry function for threads started from interpreted code.
421
- */
409
+ //pthread entry function for threads started from interpreted code.
422
410
static void* interpThreadStart(void* arg){
423
411
Thread* self = (Thread*) arg;
424
412
std::string threadName(dvmGetThreadName(self));
425
413
setThreadName(threadName.c_str());
426
414
427
- /*
428
- * Finish initializing the Thread struct.
429
- */
415
+ //Finish initializing the Thread struct.
430
416
dvmLockThreadList(self);
431
417
prepareThread(self);
432
418
@@ -440,48 +426,33 @@ static void* interpThreadStart(void* arg){
440
426
*/
441
427
self->jniEnv = dvmCreateJNIEnv(self);
442
428
443
- /*
444
- * Change our state so the GC will wait for us from now on. If a GC is
445
- * in progress this call will suspend us.
446
- */
429
+ //修改状态为THREAD_RUNNING
447
430
dvmChangeStatus(self, THREAD_RUNNING);
448
431
449
- /*
450
- * Execute the "run" method.
451
- * At this point our stack is empty, so somebody who comes looking for
452
- * stack traces right now won't have much to look at. This is normal.
453
- */
432
+ //执行run方法
454
433
Method* run = self->threadObj->clazz->vtable[gDvm.voffJavaLangThread_run];
434
+
455
435
JValue unused;
456
436
ALOGV("threadid=%d: calling run()", self->threadId);
457
437
assert(strcmp(run->name, "run") == 0);
458
438
dvmCallMethod(self, run, self->threadObj, &unused);
459
439
ALOGV("threadid=%d: exiting", self->threadId);
460
- /*
461
- * Remove the thread from various lists, report its death, and free
462
- * its resources.
463
- */
440
+
441
+ //移出线程并释放资源
464
442
dvmDetachCurrentThread();
465
443
return NULL;
466
444
}
467
445
468
- /*
469
- * Finish initialization of a Thread struct.
470
- * This must be called while executing in the new thread, but before the
471
- * thread is added to the thread list.
472
- * NOTE: The threadListLock must be held by the caller (needed for
473
- * assignThreadId()).
474
- */
446
+ //Finish initialization of a Thread struct.
475
447
static bool prepareThread(Thread* thread){
476
448
assignThreadId(thread);
477
449
thread->handle = pthread_self();
478
450
thread->systemTid = dvmGetSysThreadId();
479
451
setThreadSelf(thread);
480
452
return true;
481
453
}
482
- /*
483
- * Explore our sense of self. Stuffs the thread pointer into TLS.
484
- */
454
+
455
+ //Explore our sense of self. Stuffs the thread pointer into TLS.
485
456
static void setThreadSelf(Thread* thread){
486
457
int cc;
487
458
cc = pthread_setspecific(gDvm.pthreadKeySelf, thread);
0 commit comments