深入线程的wait()/notify()

浅谈线程间的协作机制-Object的wait()/notify()及Condition的await()/signal() 注意:如无特殊说明,本文源码分析基于的Java JDK版本均为1.8。 等待/通知机制 假设这样的情景,只要线程A修改了某值value,则线程B则对新的value值进行某些操作,比较容易想到的方法是,线程B不断循环访问value,一旦感知到变化,则执行相应逻辑。 // 线程A set value = newValue // 线程B for(;;){ while(newValue != oldV... 阅读详情

        今天闲来无事写了一个活动对象的测试代码,谁知道一运行死活跑不起来.程序执行到SetActive()就自动退出.硬是把这个简单的例子搞了2个小时. 后来才知道原来Carbide.vs提供的Active框架没有重写NewL()或者NewLC()方法,而我直接调用new (ELeave) CActiveTest2NewClass;并没有初始化ConstructL()里面的数据.编译也不报错,Debug跟踪却是下面一行出问题.郁闷的要命.

       下面的代码就是今天的收获:

CActiveTest2AppUi from ActiveTest2Appui.h
/*
============================================================================
 Name        : CActiveTest2AppUi from ActiveTest2Appui.h
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : Declares UI class for application.
============================================================================
*/

#ifndef ACTIVETEST2APPUI_H
#define ACTIVETEST2APPUI_H

// INCLUDES
#include

// FORWARD DECLARATIONS
class CActiveTest2Container;
class CActiveTest2NewClass;


// CLASS DECLARATION

/**
* Application UI class.
* Provides support for the following features:
* - EIKON control architecture
*
*/
class CActiveTest2AppUi : public CAknAppUi
    {
    public: // // Constructors and destructor

        /**
        * EPOC default constructor.
        */     
        void ConstructL();

        /**
        * Destructor.
        */     
        ~CActiveTest2AppUi();
       
    public: // New functions

    public: // Functions from base classes

    private:
        // From MEikMenuObserver
        void DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane);

    private:
        /**
        * From CEikAppUi, takes care of command handling.
        * @param aCommand command to be handled
        */
        void HandleCommandL(TInt aCommand);

        /**
        * From CEikAppUi, handles key events.
        * @param aKeyEvent Event to handled.
        * @param aType Type of the key event.
        * @return Response code (EKeyWasConsumed, EKeyWasNotConsumed).
        */
        virtual TKeyResponse HandleKeyEventL(
            const TKeyEvent& aKeyEvent,TEventCode aType);

    private: //Data
        CActiveTest2Container* iAppContainer;
  CActiveTest2NewClass* iTest2NewClass;
    };

#endif

CActiveTest2AppUi from ActiveTest2Appui.cpp
/*
============================================================================
 Name        : CActiveTest2AppUi from ActiveTest2Appui.cpp
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : CActiveTest2AppUi implementation
============================================================================
*/

// INCLUDE FILES
#include "ActiveTest2Appui.h"
#include "ActiveTest2Container.h"
#include
#include "ActiveTest2.hrh"

#include "ActiveTest2NewClass.h"

#include
#include

// ================= MEMBER FUNCTIONS =======================
//
// ----------------------------------------------------------
// CActiveTest2AppUi::ConstructL()
//
// ----------------------------------------------------------
//
void CActiveTest2AppUi::ConstructL()
    {
    BaseConstructL();
    iTest2NewClass = CActiveTest2NewClass::NewL();
    iAppContainer = new (ELeave) CActiveTest2Container;
    iAppContainer->SetMopParent( this );
    iAppContainer->ConstructL( ClientRect() );
    AddToStackL( iAppContainer );
    }

// ----------------------------------------------------
// CActiveTest2AppUi::~CActiveTest2AppUi()
// Destructor
// Frees reserved resources
// ----------------------------------------------------
//
CActiveTest2AppUi::~CActiveTest2AppUi()
    {
    if (iAppContainer)
        {
        RemoveFromStack( iAppContainer );
        delete iAppContainer;
        }
 if(iTest2NewClass)
 {
  delete iTest2NewClass;
 }
   }

// ------------------------------------------------------------------------------
// CActiveTest2AppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
//  This function is called by the EIKON framework just before it displays
//  a menu pane. Its default implementation is empty, and by overriding it,
//  the application can set the state of menu items dynamically according
//  to the state of application data.
// ------------------------------------------------------------------------------
//
void CActiveTest2AppUi::DynInitMenuPaneL(
    TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/)
    {
    }

// ----------------------------------------------------
// CActiveTest2AppUi::HandleKeyEventL(
//     const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
// takes care of key event handling
// ----------------------------------------------------
//
TKeyResponse CActiveTest2AppUi::HandleKeyEventL(
    const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
    {
    return EKeyWasNotConsumed;
    }

// ----------------------------------------------------
// CActiveTest2AppUi::HandleCommandL(TInt aCommand)
// takes care of command handling
// ----------------------------------------------------
//
void CActiveTest2AppUi::HandleCommandL(TInt aCommand)
    {
    switch ( aCommand )
        {
        case EAknSoftkeyBack:
        case EEikCmdExit:
            {
            Exit();
            break;
            }
        case EActiveTest2CmdAppTest:
            {
   // Info message shown only in the emulator
   //iEikonEnv->InfoMsg(_L("test"));
   
   //Load localized message from the resource file
   //HBufC* message = CEikonEnv::Static()->AllocReadResourceLC(R_MESSAGE_TEXT);
   // Show information note dialog
   //CAknInformationNote* note = new (ELeave) CAknInformationNote;
   //note->ExecuteLD(message->Des());
   //CleanupStack::PopAndDestroy(message);
            iTest2NewClass->StartL(3000000);
            break;
            }
        // TODO: Add Your command handling code here

        default:
            break;     
        }
    }

 

ActiveTest2NewClass.h

/*
============================================================================
 Name        : ActiveTest2NewClass.h
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : CActiveTest2NewClass declaration
============================================================================
*/

#ifndef ACTIVETEST2NEWCLASS_H
#define ACTIVETEST2NEWCLASS_H

#include  // For CActive, link against: euser.lib
#include   // For RTimer, link against: euser.lib

class CActiveTest2NewClass : public CActive
{
public:
 // C++ constructor
 CActiveTest2NewClass();
 
 // Second-phase constructor
 void ConstructL();
 
 // Cancel and destroy
 ~CActiveTest2NewClass();

public: // New functions
 // Function for making the initial request
 void StartL(TTimeIntervalMicroSeconds32 aDelay);

 

//就是下面两个方法Carbide.vs没提供

 static CActiveTest2NewClass* NewLC();

 static CActiveTest2NewClass* NewL();

private: // From CActive
 // Handle completion
 void RunL();
 
 // How to cancel me
 void DoCancel();
 
 // Override to handle leaves from RunL(). Default implementation causes
 // the active scheduler to panic.
 //void RunError(TInt aError);

private:
 enum TActiveTest2NewClassState
 {
     EUninitialized, // Uninitialized
     EInitialized, // Initalized
     EError   // Error condition
 };

private:
 TInt    iState;  // State of the active object
 RTimer iTimer;  // Provides async timing service
 TInt iCount;

};

#endif

 

ActiveTest2NewClass.cpp

/*
============================================================================
 Name        : ActiveTest2NewClass.cpp
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : CActiveTest2NewClass implementation
============================================================================
*/

#include "ActiveTest2NewClass.h"

#include

CActiveTest2NewClass::CActiveTest2NewClass() : CActive(EPriorityStandard) // Standard priority
{
}

void CActiveTest2NewClass::ConstructL()
{
 User::LeaveIfError(iTimer.CreateLocal()); // Initialize timer
 CActiveScheduler::Add(this);    // Add to scheduler
}

CActiveTest2NewClass* CActiveTest2NewClass::NewLC()

{
 CActiveTest2NewClass* self = new (ELeave) CActiveTest2NewClass;
 CleanupStack::PushL(self);
 self->ConstructL();
 return self;
}


CActiveTest2NewClass* CActiveTest2NewClass::NewL()
{

 CActiveTest2NewClass* self = NewLC();
 CleanupStack::Pop(self);
 return self;
}

CActiveTest2NewClass::~CActiveTest2NewClass()
{
 Cancel(); // Cancel any request, if outstanding
 // Delete instance variables if any
}

void CActiveTest2NewClass::DoCancel()
{
 iTimer.Cancel();
}

void CActiveTest2NewClass::StartL(TTimeIntervalMicroSeconds32 aDelay)
{
 Cancel();      // Cancel any request, just to be sure
 iState = EUninitialized;
 iTimer.After(iStatus, aDelay); // Set for later
 SetActive();     // Tell scheduler a request is active
}

void CActiveTest2NewClass::RunL()
{
 if (iState == EUninitialized) {
  // Do something the first time RunL() is called
  //iState = EInitialized;
  iCount++;
  _LIT(TempString,"the iCount's value is :");
  HBufC* iBuf = HBufC::NewLC(100);
  iBuf->Des().Copy(TempString);
  iBuf->Des().AppendNum(iCount);
  CEikonEnv::Static()->InfoMsg(*iBuf);
  CleanupStack::PopAndDestroy(iBuf);

 } else if (iState != EError) {
  // Do something
 }
 iTimer.After(iStatus, 3000000); // Set for 1 sec later
 SetActive();     // Tell scheduler a request is active
}

 

另外就是以下几点收获:

1:debug到某一行出错并不代表就是这行的错误,可能是它上面一行,要看debug窗口里面红色显示的内容.

2:在epoc32/wins/c/system/Bootdata下面建一个ErrRd文件(无扩展名),可以让程序在关闭退出的时候报一个错误码.例如我的这个错误报kern-exec 0.之后可以到

http://www.symbian.com/developer/techlib/v70docs/SDL_v7.0/doc_source/reference/SystemPanics/index.html#PanicsReference%2eindex

查找这个错误的原因.



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1492232


source tree clone error:fatal remote error: Repository not found 阅读详情

相关推荐

驱动兼容_FANUC 驱动器兼容问题探讨

近10年来,FANUC伺服驱动器随着系统的升级而升级,不同的系统,可以兼容很多种类的驱动器,作者做了一些常见经验总结如下表:电源模块伺服放大器主轴放大器bi一体18i/OIB/OIC6110-Hxxx6114-Hxxx6111-Hxxx6134-Hxxx31iA/OID6140-Hxxx6117-Hxxx6141-Hxxx6164-Hxxx31IB/35IB/OIF/OI MATE D(5包)62...

weixin_34420941的博客 1758

SpringBoot集成FastDFS

FastDfsConfig @Slf4j @Configuration public class FastDfsConfig { private static final String CONFIG_FILENAME = "fastdfs-client.properties"; @Bean public TrackerClient trackerClient(){ try { //全局初始化配置文件 Cl...

Ru_yin_hai的博客 322

AT91SAM9260搭建LINUX操作系统

一、 编写目的 2 二、 系统搭建 2 1. 下载相关软件包 2 2. Bootstrap编译 3 3. U-BOOT编译 5 4. LINUX编译 7 5. 通过U-BOOT的TFTP命令从TFTP服务器下载系统镜像 9 6. 文件系统制作 10 7. 挂载NFS作为根文件系统 10 三、 总结 14 一、编写目的 本文档用于记录AT91SAM9260 LINUX学

lshiceman的专栏 7420

FastDFS启动服务第一次上传文件报错

FastDFS启动服务第一次上传文件报错: java.io.IOException: recv package size -1 != 10

yump的博客笔记 1464

从仿真到实践:两级直接耦合放大电路的静态工作点协同调试指南

本文详细解析了两级直接耦合放大电路的静态工作点协同调试方法,从仿真到实践提供系统指南。重点介绍了Rc2电阻调整技巧、集电极电位测量方法及常见问题排查,帮助工程师解决各级静态工作点相互影响的难题,提升模拟电路设计效率。

weixin_29189373的博客 130

深入剖析STM32F103 I2C总线死锁问题及高效应对策略

本文深入剖析了STM32F103硬件I2C总线在轮询模式下发生死锁的根本原因,即硬件状态机的预取机制与软件响应速度不匹配。文章提供了高效的解决方案,包括在轮询模式下提前发送NACK/STOP信号、降低总线速度,以及治本的中断与DMA应用策略,帮助开发者根据项目需求选择最合适的方案,彻底解决这一经典硬件缺陷问题。

weixin_29184371的博客 212

记一次fastdfs高并发下上传失败的问题

最近接了某需求,是需要把文字转换成语音MP3文件,存入fastdfs,然后把文件路径存入数据库。 我们fastdfs是5.05版本,fastdfs工具类已经改成支持并发了,每次获取新的服务端对象 public class FastClient<main> { private static Logger logger = Logger.getLogger(FastClient.class); /** * 只加载一次. */ static {

长腿小码农的博客 3013

AD9361 FIR 滤波器设计

matlab 得到滤波器系数

weixin_44630490的博客 1万+

testing types

testing types Following is a summary of the various tests that are performed on new and revised hardware and software. Thai Ann Loe made a major contribution to this definition.Acceptance TestThe test

Enjoy Codes-----Lijie Wang 1091

Autosar配置——导入DBC后配置SecOC同步报文和同步请求报文

在Autosar中,安全性是一个重要的考虑因素,而SecOC(Secure On-Board Communication)是一种用于保护通信数据完整性和安全性的安全机制。在上述示例中,我们配置了同步报文和同步请求报文的属性。接下来,我们需要在Autosar中配置同步报文和同步请求报文。同步报文用于在通信双方之间同步SecOC状态,而同步请求报文用于请求SecOC的状态信息。通过以上配置,我们成功地导入了DBC文件,并在Autosar系统中配置了SecOC同步报文和同步请求报文。希望以上信息对您有所帮助!

CyberSphinx的博客 656

Unity零基础到进阶 ☀️| Unity中的 GC及优化 超级全面解析 ☆(ゝω・)v 建议收藏!

介绍: 在游戏运行的时候,数据主要存储在内存中,当游戏的数据在不需要的时候,存储当前数据的内存就可以被回收以再次使用。内存垃圾是指当前废弃数据所占用的内存,垃圾回收(GC)是指将废弃的内存重新回收再次使用的过程。 Unity中将垃圾回收当作内存管理的一部分,如果游戏中废弃数据占用内存较大,则游戏的性能会受到极大影响,此时垃圾回收会成为游戏性能的一大障碍点。 本文我们主要学习垃圾回收的机制,垃圾回收如何被触发以及如何提GC收效率来提高游戏的性能。 Unity内存管理机制简介 要想了解垃圾回收如何工作以及何时被

努力前行,总会成为自己心中的那道光 6万+

FastDFS(2)java客户端

public class FastDFSOperation { private static boolean hasInited = false; private static void Init() { synchronized (TXFastDFSOperation.class) { if (hasInited)

祈雨v的博客 409

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

线程之wait()/notify()/notifyAll()/sleep()/join()/yield()

wait()/notify()/notifyAll() 方法来自object类,必须在synchronized中执行,否则会抛异常 wait() 调用该方法会使当前线程处于等待唤醒状态,释放对象锁以及占用的资源,交给其它线程使用,该线程会加入等待唤醒队列中, notify()/notifyAll() 用于从等待唤醒队列中唤醒线程。notify()是唤醒队列中的头一个;notif...

meijing11的博客 212

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

java使用轮询和wait()/notify()实现多线程之间的通信

使用轮询方式实现通信: public class MyList { private static int i = 0; public int getI() { return i; } public void setI(int i) { MyList.i = i; } private volatile List

Aierbude的博客 2449

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Java多线程8:wait()和notify()/notifyAll()

一、轮询   线程本身是操作系统中独立的个体,但是线程与线程之间不是独立的个体,因为它们彼此之间要相互通信和协作。   想像一个场景,A线程做int型变量i的累加操作,B线程等待i到了10000就打印出i,怎么处理?一个办法就是,B线程while(i == 10000),这样两个线程之间就有了通信,B线程不断通过轮训来检测i == 10000这个条件。   这样可以实现我们的需求,但是也带来...

weixin_30569001的博客 115

Java多线程:wait()和notify()/notifyAll()

notify()的作用是,如果有多个线程等待,那么线程规划器随机挑选出一个wait的线程,对其发出通知notify(),并使它等待获取该对象的对象锁。注意"等待获取该对象的对象锁",这意味着,即使收到了通知,wait的线程也不会马上获取对象锁,必须等待notify()方法的线程释放锁才可以。和wait()一样,notify()也要在同步方法/同步代码块中调用。当然,唤醒的顺序不重要,因为notifyAll()把处于同一资源下wait的线程全部唤醒,至于唤醒的顺序,就和线程启动的顺序一样,是虚拟机随机的。

qq_38470315的博客 1828

多线程_wait()/notify()

通常,多线程之间需要协调工作。例如,浏览器的一个显示图片的线程displayThread想要执行显示图片的任务,必须等待下载线程downloadThread将该图片下载完毕。如果图片还没有下载完,displayThread可以暂停,当downloadThread完成了任务后,再通知displayThread“图片准备完毕,可以显示了”,这时,displayThread继续执行。    以上逻辑简单

VisionLee's 窝 650

wait放弃对象锁_深入线程的 wait()/notify()

sleep()方法是使线程停止一段时间的方法。在sleep 时间间隔期满后,线程不一定立即恢复执行。这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非(a)"醒来"的线程具有更高的优先级。 (b)正在运行的线程因为其它原因而阻塞。 wait()是线程交互时,如果线程对一个同步对象x 发出一个wait()调用,该线程会暂停执行,被调对象进入等待状态,直到被唤醒或等待时间到。 ...

weixin_33632413的博客 233

深入线程的 wait()/notify()

sleep()方法是使线程停止一段时间的方法。在sleep时间间隔期满后,线程不一定立即恢复执行。这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非(a)"醒来"的线程具有更高的优先级。 (b)正在运行的线程因为其它原因而阻塞。  wait()是线程交互时,如果线程对一个同步对象x发出一个wait()调用,该线程会暂停执行,被调对象进入等待状态,直到被唤醒或等待时间到。

码农之道 624

Java多线程-关键字-3.wait()与notify()/notifyAll()

 Java多线程-关键字-3.wait()与notify()/notifyAll()

lreis2010 114
上一篇: 基于回合制的在线棋类对战游戏(图)
下一篇: 一个手机上的网络聊天游戏(图)
techcrunch
博客等级 码龄19年 34粉丝 3441原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址