@@ -33,7 +33,8 @@ Android设计模式源码解析之命令模式
3333## 3. 模式的简单实现
3434### 简单实现的介绍
3535命令模式其实就是对命令进行封装,将命令请求者和命令执行者的责任分离开来实现松耦合。
36- ` 阐述一下你的示例要实现的功能 `
36+ 这里我们通过一个简单的实例来剖析一下命令模式:命令接收者ReceiverRole拥有一个PeopleBean类型成员,通过Invoker发出不同的命令来修改PeopleBean的相对应的属性,具体命令实现类ConcreteCommandImpl1执行修改年龄命令,ConcreteCommandImpl2执行修改姓名的命令等等,ClientRole负责组装各个部分。
37+ 例子代码如下(resource目录下也可以查看)。
3738
3839### 实现源码
3940
@@ -337,13 +338,6 @@ public class Thread implements Runnable {
337338 Runnable target;
338339 private static int count = 0;
339340
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- */
347341 public synchronized void start() {
348342 if (hasBeenStarted) {
349343 throw new IllegalThreadStateException("Thread already started."); // TODO Externalize?
@@ -397,10 +391,6 @@ bool dvmCreateInterpThread(Object* threadObj, int reqStackSize){
397391
398392}
399393
400- /*
401- * Alloc and initialize a Thread struct.
402- * Does not create any objects, just stuff on the system (malloc) heap.
403- */
404394static Thread* allocThread(int interpStackSize)
405395{
406396 Thread* thread;
@@ -416,17 +406,13 @@ static Thread* allocThread(int interpStackSize)
416406接下来在12行通过pthread_create创建pthread线程,并让这个线程start,这样就会进入该线程的thread entry运行,下来我们来看新线程的thread entry方法 interpThreadStart,同样只列出关键的地方:
417407
418408```
419- /*
420- * pthread entry function for threads started from interpreted code.
421- */
409+ //pthread entry function for threads started from interpreted code.
422410static void* interpThreadStart(void* arg){
423411 Thread* self = (Thread*) arg;
424412 std::string threadName(dvmGetThreadName(self));
425413 setThreadName(threadName.c_str());
426414
427- /*
428- * Finish initializing the Thread struct.
429- */
415+ //Finish initializing the Thread struct.
430416 dvmLockThreadList(self);
431417 prepareThread(self);
432418
@@ -440,48 +426,33 @@ static void* interpThreadStart(void* arg){
440426 */
441427 self->jniEnv = dvmCreateJNIEnv(self);
442428
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
447430 dvmChangeStatus(self, THREAD_RUNNING);
448431
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方法
454433 Method* run = self->threadObj->clazz->vtable[gDvm.voffJavaLangThread_run];
434+
455435 JValue unused;
456436 ALOGV("threadid=%d: calling run()", self->threadId);
457437 assert(strcmp(run->name, "run") == 0);
458438 dvmCallMethod(self, run, self->threadObj, &unused);
459439 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+ //移出线程并释放资源
464442 dvmDetachCurrentThread();
465443 return NULL;
466444}
467445
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.
475447static bool prepareThread(Thread* thread){
476448 assignThreadId(thread);
477449 thread->handle = pthread_self();
478450 thread->systemTid = dvmGetSysThreadId();
479451 setThreadSelf(thread);
480452 return true;
481453}
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.
485456static void setThreadSelf(Thread* thread){
486457 int cc;
487458 cc = pthread_setspecific(gDvm.pthreadKeySelf, thread);
0 commit comments