首页 \ 问答 \ while(condition){Object.wait()}成语(while(condition) { Object.wait() } idiom)

while(condition){Object.wait()}成语(while(condition) { Object.wait() } idiom)

我知道,我们使用这个成语来等待通知来处理虚假唤醒:

synchronized (obj) {
    while(somecond)
        obj.wait();
}

如果出现虚假唤醒,我们只需检查状态并返回等待状态。

但是,考虑一下情况:

  1. 我们开始等待,obj.wait()释放对obj的锁定。
  2. 等待线程被操作系统虚假通知
  3. 我们返回到检查状态(由于等待而释放对象锁)
  4. obj.notify()在那一刻被调用。

是的,条件检查速度非常快,而且我们可以进行条件检查,而不是在obj.wait() ,这些检查的obj.wait()很小,可以忽略不计。 在这种情况下,我们可以obj.notify()调用。

我误解了什么,或者我们真的可以使用这种模式发布通知?


I know, that we use this idiom for waiting for notification to handle spurious wakeups:

synchronized (obj) {
    while(somecond)
        obj.wait();
}

If a spurious wake up arises, we'll just check the state and return back to waiting.

But, consider the situation:

  1. We begin waiting, and obj.wait() releases lock on obj.
  2. Waiting thread is spuriously notified by OS
  3. We return to checking condition (with obj lock released due to wait)
  4. obj.notify() is called right in that moment.

Yes, condition checking is extremely fast and chances, that we can be in condition checking and not in obj.wait(), are negligibly small. In that case we can loose obj.notify() call.

Am I misunderstanding something, or we really can loose notification using this pattern?


原文:https://stackoverflow.com/questions/21721401
更新时间:2023-09-17 13:09

最满意答案

当我最初试图让Bootstrap popovers工作时,我(当然,仍然有)很多缺失的上下文理解。

首先是:Bootstrap“Popover”插件实际上是一个JQuery插件 。 我假设所有的Bootstrap插件都是如何工作的,但我找不到任何关于这个的Bootstrap介绍性文档。 这就解释了popover()方法及其来源。

下面,我概述了使弹出窗口在React / Typescript / Webpack堆栈的上下文中工作所需的内容。

在下面,我假设你按照Bootstrap doco配置了Webpack。

您不需要原始问题中的“Popover”和“Tooltip”行,假设您的webpack.config.js是按照Bootstrap doco并且您已import 'bootstrap'; 代码库中的某个地方。


您需要添加JQuery类型(如果尚未存在),以便您可以导入$并使用正确的类型:

"devDependencies": {
   "@types/jquery": "3.2.15",
   ...
}

"dependencies": {
  "bootstrap": "4.0.0-beta",
  "jquery": "3.2.1",
  "popper.js": "1.11.0",
  ...
}

根据Netanel Basal的博客文章,您必须扩展JQuery的类型定义,以便它了解Popover插件。 在typings.d.ts (或项目中的任何地方),添加以下内容:

// support for JQuery popover plugin from Bootstrap 4
interface JQuery {
  popover() : any;
}

请注意,定义是您以极其静态方式从代码中获取弹出窗口所需的最低限度。 需要扩展它以支持传递参数,以便您可以自定义弹出行为。 或者您可以使用data属性来传递这些参数,如doco中的Live示例所示

在React组件本身中,从问题中声明popover的JSX似乎工作正常。

要根据问题“初始化”弹出窗口,您需要导入JQuery标识符,然后调用popover方法:

...
const $ = require('jquery'); 
...
componentDidMount(): void{
  $('[data-toggle="popover"]').popover();
}
...

“导入表单” 对我不起作用 ,所以我不得不require()它。

该代码在整个HTML页面中搜索带有data-toggle="popover"元素,并返回一个JQuery对象,该对象具有可以调用的popover()方法(这是整个JQuery插件部分)。

一旦在具有弹出属性的元素上调用了popover popover() ,单击该元素时将自动显示弹出窗口(不需要管理特定于弹出窗口的React状态)。


编辑

如上所示,在整个HTML页面中搜索所有弹出窗口并不是一个好主意。 在一个复杂的页面中,在多个React组件中有多个popover() ,每个组件最终会覆盖彼此的popover()选项。

这是我目前可重用的React bootstrap Popover组件的解决方案。

扩展Typescript类型以了解更多popover 选项

// support for JQuery popover plugin from Bootstrap 4
interface JQuery {
  popover(options?: PopoverOptions) : any;
}

interface PopoverOptions {
  container?: string | Element | boolean;
  content?: string | Element | Function;
  placement?: "auto" | "top" | "bottom" | "left" | "right" | Function;
  title?: string | Element | Function;
  ...
}

Popover.tsx这样创建Popover.tsx

export interface PopoverProps {
  popoverTitle: string | Element | Function;
  popoverContent: string | Element | Function;
}

export class Popover
extends PureComponent<PopoverProps, object> {

  selfRef: HTMLSpanElement;

  componentDidMount(): void{
    $(this.selfRef).popover({
      container: this.selfRef,
      placement: "auto",
      title: this.props.popoverTitle,
      content: this.props.popoverContent,
    });
  }

  render(){
    return <span
      ref={(ref)=>{if(ref) this.selfRef = ref}} 
      data-toggle="popover"
    >
      {this.props.children}
    </span>;

  }
}

然后使用popover像:

<Popover 
  popoverTitle="The popover title"
  popoverContent="The popover content"
>
  <span>
    Click this to show popover.
  </span>
</Popover>

注意与使用动态内容的Popover定位相关的问题: Bootstrap 4 - 自动Popover重新定位如何工作?


I had (still have, undoubtedly) a lot of missing contextual understanding when I was initially trying to get Bootstrap popovers working.

First thing is: the Bootstrap "Popover" plugin is really a JQuery plugin. I assume that's how all the Bootstrap plugins work, but I couldn't find any Bootstrap introductory documentation about this. So that explains the popover() method and where it comes from.

Below, I've outlined what is needed to make the popovers work in the context of a React / Typescript / Webpack stack.

In the following, I'm assuming you've configured Webpack as per the Bootstrap doco.

You don't need the "Popover" and "Tooltip" lines from the original question, assuming you're webpack.config.js is as per the Bootstrap doco and you have import 'bootstrap'; somewhere in your codebase.


You need to add the JQuery typings, if not already present, so that you can import $ and have the right types:

"devDependencies": {
   "@types/jquery": "3.2.15",
   ...
}

"dependencies": {
  "bootstrap": "4.0.0-beta",
  "jquery": "3.2.1",
  "popper.js": "1.11.0",
  ...
}

You have to extend the type definition of JQuery so that it knows about the Popover plugin, as per this blog article by Netanel Basal. In typings.d.ts (or wherever makes sense in your project), add the following:

// support for JQuery popover plugin from Bootstrap 4
interface JQuery {
  popover() : any;
}

Note that definition is the bare minimum you need to get popovers working from your code in a statically typed way. It needs to be extended to support passing parameters so that you can customise the popover behaviour. Or you could use the data attributes to pass these parameters, as per the Live example in the doco.

In the React component itself, the JSX to declare the popover from the question seems to work fine.

To "initialize" the popover, as per the question, you need to import the JQuery identifier and then call the popover method:

...
const $ = require('jquery'); 
...
componentDidMount(): void{
  $('[data-toggle="popover"]').popover();
}
...

The "import form" didn't work for me, so I had to require() it.

That code searches the entire HTML page for elements with data-toggle="popover", and returns a JQuery object that has the popover() method you can call (that's the whole JQuery plugin part).

Once popover() has been called on the element with the popover attributes, popovers will be automatically displayed when the element is clicked (there's no need to manage popover-specific React state).


EDIT

It's not a good idea to search the entire HTML page for all popovers as shown above. In a complicated page with multiple popovers in multiple React components, each component would end up overwriting each other's popover() options.

Here's my current solution for a re-usable React bootstrap Popover component.

Extend the Typescript typings to understand more popover options:

// support for JQuery popover plugin from Bootstrap 4
interface JQuery {
  popover(options?: PopoverOptions) : any;
}

interface PopoverOptions {
  container?: string | Element | boolean;
  content?: string | Element | Function;
  placement?: "auto" | "top" | "bottom" | "left" | "right" | Function;
  title?: string | Element | Function;
  ...
}

Create Popover.tsx like:

export interface PopoverProps {
  popoverTitle: string | Element | Function;
  popoverContent: string | Element | Function;
}

export class Popover
extends PureComponent<PopoverProps, object> {

  selfRef: HTMLSpanElement;

  componentDidMount(): void{
    $(this.selfRef).popover({
      container: this.selfRef,
      placement: "auto",
      title: this.props.popoverTitle,
      content: this.props.popoverContent,
    });
  }

  render(){
    return <span
      ref={(ref)=>{if(ref) this.selfRef = ref}} 
      data-toggle="popover"
    >
      {this.props.children}
    </span>;

  }
}

Then use the popover like:

<Popover 
  popoverTitle="The popover title"
  popoverContent="The popover content"
>
  <span>
    Click this to show popover.
  </span>
</Popover>

Beware issues related to Popover positioning with dynamic content: Bootstrap 4 - how does automatic Popover re-positioning work?

相关问答

更多
  • 基于我在我的bootstrap.css文件中,默认是一个max-width属性。 我用这个 .popover { position: absolute; top: 0; left: 0; z-index: 1010; display: none; max-width: 600px; padding: 1px; text-align: left; white-space: normal; background-color: #ffffff; border: 1px ...
  • 谢谢..通过覆盖模板来实现它 showpopover=function(message,context){ var popover_message="
    "+message+"
    "; //$(a).hide(); $popover=$(context).popover({ placement:"bottom",trigger:"hover",template: '
  • 当我最初试图让Bootstrap popovers工作时,我(当然,仍然有)很多缺失的上下文理解。 首先是:Bootstrap“Popover”插件实际上是一个JQuery插件 。 我假设所有的Bootstrap插件都是如何工作的,但我找不到任何关于这个的Bootstrap介绍性文档。 这就解释了popover()方法及其来源。 下面,我概述了使弹出窗口在React / Typescript / Webpack堆栈的上下文中工作所需的内容。 在下面,我假设你按照Bootstrap doco配置了Webpac ...
  • 与bootstrap 3选择不正常相似, 所选行为需要在内容准备好后初始化。 与模态事件类似,您可以使用popover事件: $('#thing').popover( // content, title, etc... ).on('shown.bs.popover', function () { $('.chosen-select').chosen(); }).on('hidden.bs.popover', function () { // Destroy when the popover is ...
  • 我想出了解决方案。 制作popover时,bootstrap会在父容器中生成div元素。 显然,当它在svg里面时,它不能正常工作。 所以这里是解决方案,给它一个data-container集作为body你也可以去掉一个元素,然后直接将它添加到circle元素。
    如果将popover绑定到具有data-toggle=popover所有元素,则需要销毁不应显示弹出框的元素的弹出窗口。 调用$('[data-toggle=popover]').not('[data-nopopover=1]').popover(); 将尝试再次初始化没有data-nopopover属性的元素上的data-nopopover 。 具有data-nopopover的元素仍将附加data-nopopover 。 你应该调用$('[data-nopopover=1]').popover('des ...
  • 我们使用的反应引导程序需要按顺序前缀才能正常工作。 所以我需要进入react-bootstrap和前缀bt3- ,带有bt3-前缀的arrow 。 然后所有样式都被导入。 The react bootstrap we use needs to be prefixed in-order to work properly. SO I needed to go into react-bootstrap and prefix popover, arrow, with bt3- prefix. Then all th ...
  • 它有点难以看到你想要做什么,但你不应该使用positionLeft和positionTop道具,它们将由Overlay组件设置 如果您希望弹出窗口出现在中间,请使用placement道具和值"top"或"bottom"而不是"right" 如果你想要更细粒度地控制它的位置和位置,你需要制作自己的自定义Popover组件,该组件使用由Overlay组件传递给它的positionLeft和positionTop组件。 class MyPopover extends React.Component { ren ...
  • 您需要在页面中包含bootstrap javascript,bootstrap css和jQuery javascript。 它们可以在这里下载: http://twitter.github.com/bootstrap/assets/bootstrap.zip http://code.jquery.com/jquery-1.9.1.js 对于JSFiddle,您可以使用左侧的Frameworks and Extensions菜单和其他文件(如bootstrap的js和css)包含jQuery,使用左侧的Ex ...
  • 您应该使用Google地图信息窗口而不是弹出框,请参阅下面的基本示例 var infoContent = "

    Placeholder Information

    "; var infoWindow = new google.maps.InfoWindow({ content: infoContent }); marker.addListener('click', function () { infoWindow.open(map, marker); }); You shoul ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)