GVKun编程网logo

with(nolock)或(nolock)-有区别吗?(nolock和with nolock)

22

本文将为您提供关于with的详细介绍,我们还将为您解释nolock或的相关知识,同时,我们还将为您提供关于CSS中的display属性(none,block,inline,inline-block,i

本文将为您提供关于with的详细介绍,我们还将为您解释nolock或的相关知识,同时,我们还将为您提供关于CSS中的display属性(none,block,inline,inline-block,inherit)、Entity Framework with NOLOCK、if(null check)-else vs try catch(NullPointerException)哪个更有效?、Linux 驱动 ——Button 驱动 6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞的实用信息。

本文目录一览:

with(nolock)或(nolock)-有区别吗?(nolock和with nolock)

with(nolock)或(nolock)-有区别吗?(nolock和with nolock)

一切都基于with(nolock)完全适合该情况的假设。 已经有很多关于是否使用with(nolock)的问题。

我环顾四周,但无法确定使用之间是否存在实际差异with(nolock)

select customer,zipcode from customers c with(nolock)

或只是(nolock)

select customer,zipcode from customers c (nolock)

两者之间在功能上有区别吗?风格?
一个人比另一个人大,并且有可能被弃用吗?

CSS中的display属性(none,block,inline,inline-block,inherit)

CSS中的display属性(none,block,inline,inline-block,inherit)

css中的display属性(none,block,inline,inline-block,inherit)

display属性是我们在前端开发中常常使用的一个属性,其中,最常见的有:

  • none
  • block
  • inline
  • inline-block
  • inherit

  下面,我将按照顺序将上述几种属性做一个完整的讲解。

第一部分:display:none

  none这个值表示此元素将不被显示

比如,当我们在浏览网页时,如果看到了某个烦人的广告遮挡了我们的实现,更为可气的是,它还没有关闭的选项,这时(以chrome为例),我们就可以按下F12,打开开发者工具,点击element,然后使用左上角的选择工具选中想要删除的广告,可以看到element中会有高亮的一行或几行代码,右键,点击Add Attribute,然后输入:,这时就可以发现广告不见啦! 当然display:none的用法绝不是专门用于这里的,它还可以用于二级下拉菜单的制作中将二级下拉菜单先设置位display:none;,当鼠标滑过一级菜单时,再显示出来(详见《如何实现导航菜单中的二级下拉菜单》)。还可以用于登陆模态框的制作等等。
详情

第二部分:display:block

  使用了display:block;之后, 此元素将显示为块级元素,此元素前后会带有换行符。我们先来回顾以下块级元素是什么,他有什么特点。

  既然要区分块级元素和行内元素,就得先说说标准文档流了。标准文档流:简称标准流,指的是在不使用其他的与排列和定位相关的css规则时,各种元素的排列规则。于是,我们将“各种元素”分为块级元素和行内元素。(注:实际上还有空元素,如<br>用于换行,<hr>为一条水平线,这里对空元素不做过多讨论)

  块级元素特点:

  • 总是以一个块的形式表现出来,占领一整行。若干同级块元素会从上之下依次排列(使用float属性除外)。
  • 可以设置高度、宽度、各个方向外补丁(margin)以及各个方向的内补丁(padding)。
  • 当宽度(width)缺省时,它的宽度时其容器的100%,除非我们给它设定了固定的宽度。
  • 块级元素中可以容纳其他块级元素或行内元素。
  • 常见的块级元素由<p><div><h1><li>等等。
  • 块级元素的display属性值默认为block。

  行内元素特点:

  • 它不会单独占据一整行,而是只占领自身的宽度和高度所在的空间。若干同级行内元素会从左到右(即某个行内元素可以和其他行内元素共处一行),从上到下依次排列。
  • 行内元素不可以设置高度、宽度,其高度一般由其字体的大小来决定,其宽度由内容的长度控制。
  • 行内元素只能设置左右的margin值和左右的padding值,而不能设置上下的margin值和上下的padding值。因此我们可以通过设置左右的padding值来改变行内元素的宽度。
  • 常见的行内元素由<a><em><img>等等。
  • 行内元素一般不可以包含块级元素。
  • 块级元素的display属性值默认为inline。

  ok!简单回顾了块级元素和行内元素之后,我们就可以进行下一步讲解了。

通过对一个行内元素设置display: block;可以将行内元素设置为块级元素,进而设置它的宽高和上下左右的padding和margin。 

应用:

       如果我们经常会制作导航栏,这时就要使用ul li 和a组合的方式,但是<a>是行内元素,我们无法设置它的宽和高,这时,就可以在<a>的样式表中,将之设置为display:block。这样就可以设置它的宽和高,以及上下左右的margin和padding以达到我们想要的效果了。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{padding: 0;margin:0;list-style: none;}
        ul li{float: left;}
        a{display:block;width: 30px;height: 30px;background: yellow;margin: 5px; text-decoration: none;text-align: center;line-height: 30px;}
    </style>
</head>
<body>
    <ul>
        <li><a href="">1</a></li>
        <li><a href="">2</a></li>
        <li><a href="">3</a></li>
        <li><a href="">4</a></li>
        <li><a href="">5</a></li>
    </ul>
</body>
</html>

效果如下:

第三部分:display:inline

  此元素会被显示为内联元素,元素前后没有换行符。在第二部分中,我们介绍了行内元素和块级元素。显然,display:inline的作用即可以将一个块级元素转换成行内元素,那么这个块级元素将不能再设置宽和高以及上下方向的margin和padding。

  

第四部分:display:inline-block

  display:inline-block 实际上我们就可以才出来它是结合了inline和block的特性于一身。即设置了inline-block属性的元素既具有block元素可以设置width和height属性的特性,又保持了inline元素不换行的特性。

举例说明,我们之前在做横向导航菜单的时候,一般是用ul li a组合,然后将li设置为float,这样就可以得到横向的导航标签了。而现在我们可以通过li和display:inline-block;来实现。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        ul,li,a,*{padding:0; margin:0;list-style: none;text-decoration: none;}
        li{display: inline-block;border: thin solid red;}
    </style>
</head>
<body>
    <ul>
        <li><a href="">1</a></li>
        <li><a href="">2</a></li>
        <li><a href="">3</a></li>
        <li><a href="">4</a></li>
        <li><a href="">5</a></li>
    </ul>
</body>
</html>

效果如下:

确实实现了横向的排列,实际上,也可以设置宽和高,大家可以自己尝试。但是,很明显有一个问题---我将padding和margin的值都设置为0,为什么他们之间还会有距离呢?实际上,这是inline元素自身出现的问题,而inline-block结合了inline和block属性,当然也就存在这个问题了。这些空隙是空白符,在浏览器中,空白符是不会被浏览器忽略的,多个连续的空白符浏览器会自动将其合并成一个。我们编写代码时写的空格,换行都会产生空白符。所以自然而然的两个元素之间会有空白符,如果将上述例子中的a标签写成一行,空白符消失,菜单之间也就紧凑起来了。

解决方法:我们要明白空白符归根结底是一个字符,只要我们将ul中的字符的大小设置位0,那么空白符也就不会存在了,但是这是a的字体大小也会继承ul的字体大小,那么就不见了,该怎么办,只需要将a中再设置一个字体不为0的大小覆盖即可。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        ul{font-size: 0;}
        ul,li,a,*{padding:0; margin:0;list-style: none;text-decoration: none;}
        li{display: inline-block;border: thin solid red;font-size: 15px;}
    </style>
</head>
<body>
    <ul>
        <li><a href="">1</a></li>
        <li><a href="">2</a></li>
        <li><a href="">3</a></li>
        <li><a href="">4</a></li>
        <li><a href="">5</a></li>
    </ul>
</body>
</html>

效果如下:

我们还可以通过inline-block完成一个常见的三列布局。

html代码如下:

<div id="header">我是header</div>
<div id="content">
    <div id="left">我是left</div>
    <div id="center">我是center</div>
    <div id="right">我是right</div>
</div>
<div id="footer">我是footer</div>

css代码如下:

#header,#footer{height: 50px;width:800px;margin:0 auto;background: #ccc;text-align: center;line-height: 50px;}
#content{width:800px;height: 500px;margin: 0 auto;background: #aaa;font-size: 0;}
#left,#right{display: inline-block;width: 200px;height: 500px;background: #fadaac;font-size: 30px;text-align: center;line-height: 500px}
#center{display: inline-block;width: 400px;height: 500px;background: #dadada;font-size: 30px;text-align: center;line-height: 500px}

第五部分:display:inherit

规定应该从父元素继承 display 属性的值。举例如下:

html代码如下:

<div id="parent">
    <div id="first_son"></div>
    <div id="second_son"></div>
</div>

css代码如下:

#parent{ display: inline-block; font-size: 0; background: #cadafa;width: 400px;height: 300px;}
#first_son{display: inherit; background: #eaedac;width: 200px;height: 100px;}
#second_son{display: inherit; background: #da5dd8;width: 200px;height: 100px;}

效果图如下:

即我们先设置了id为parent的元素的display属性值为inline-block,然后将其子元素的display属性值设置为inherit(继承),于是,子div的display属性值继承了父元素的display属性值为inline-block。(注意,我在id为parent的div元素中设置了font-size:0px;这样可以有效解决由inline元素带来的空白符问题。)

 

Entity Framework with NOLOCK

Entity Framework with NOLOCK

/// <summary>
    /// 类似SqlServer nolock 查询扩展
    /// Like SqlServer Nolock
    /// </summary>
    public static class NoLockQuery
    {
        public static List<T> ToListReadUncommitted<T>(this IQueryable<T> query)
        {
            using (var scope = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions()
                {
                    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                }))
            {
                List<T> toReturn = query.ToList();
                scope.Complete();
                return toReturn;
            }
        }

        public static int CountReadUncommitted<T>(this IQueryable<T> query)
        {
            using (var scope = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions()
                {
                    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                }))
            {
                int toReturn = query.Count();
                scope.Complete();
                return toReturn;
            }
        }

        public static async Task<List<T>> ToListReadUncommittedAsync<T>(this IQueryable<T> query)
        {
            using (var scope = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions()
                {
                    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                },
                  TransactionScopeAsyncFlowOption.Enabled
                ))
            {
                List<T> toReturn = await query.ToListAsync();
                scope.Complete();
                return toReturn;
            }
        }

        public static async Task<int> CountReadUncommittedAsync<T>(this IQueryable<T> query)
        {
            using (var scope = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions()
                {
                    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                },
                 TransactionScopeAsyncFlowOption.Enabled
                ))
            {
                int toReturn = await query.CountAsync();
                scope.Complete();
                return toReturn;
            }
        }
    }
项目中使用EntityFramework,可以使用下面这段代码进行nolock查询:需要添加System.Transactions程序集的引用

如果封装的话,执行的时候,就需要传一个代码片段进去,委托在这种情况就派上用场了,我们可以使用委托来改进一下,也就是查询数据库时候的逻辑代码代由委托传递进去。
public static void NoLockInvokeDB(Action action)
        {
            var transactionOptions = new System.Transactions.TransactionOptions();
            transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            using (var transactionScope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOptions))
            {
                try
                {
                    action();
                }
                finally
                {
                    transactionScope.Complete();
                }
            }
        }

调用:

NoLockInvokeDB(() =>
{
   using (var db = new TicketDB())
   {
      lst = db.User.ToList();
   }
});

 

if(null check)-else vs try catch(NullPointerException)哪个更有效?

if(null check)-else vs try catch(NullPointerException)哪个更有效?

以下三个功能中哪个更有效?

  public String getmConnectedDeviceName1() {        if(null != mServerDevice) {            return mServerDevice.getName();        }        else {            return null;        }    }  public String getmConnectedDeviceName2() {        return mServerDevice == null ? null : mServerDevice.getName();    }  public String getmConnectedDeviceName3() {        try{            return mServerDevice.getName();        }        catch(NullPointerException e) {            return null;        }    }

请以可接受的具体逻辑答复。

答案1

小编典典

前者是更有效时,mServerDevicenull。两者何时mServerDevicenull相同。与的比较null只是两个32位整数的比较,这是非常快速的运算。抛出异常的代价很高,因为应该创建新对象并且应该填充堆栈跟踪。

三进制运算符... ? ... : ...if (...) ... else ...语句完全一样有效,因为两者都被转换为相同的字节码。

Linux 驱动 ——Button 驱动 6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞

Linux 驱动 ——Button 驱动 6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞

button_drv.c 驱动文件:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h> 
#include <asm/uaccess.h> 
#include <linux/device.h> 
#include <asm/arch/regs-gpio.h> 
#include <linux/irq.h> 
#include <asm-arm/irq.h> 
#include <linux/interrupt.h> 
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>


#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"

int major;

static DECLARE_MUTEX(button_lock); // 定义互斥锁

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

struct class *button_class;
struct class_device *button_class_device;

unsigned char ev_press;
DECLARE_WAIT_QUEUE_HEAD(button_waitq); 

struct fasync_struct *button_fasync; 

unsigned char keyVal;

struct pin_desc{
  unsigned int pin;
  unsigned int key_val;
};
struct pin_desc pins_desc[4] = {
  {S3C2410_GPF0, 0x01},
  {S3C2410_GPF2, 0x02},
  {S3C2410_GPG3, 0x03},
  {S3C2410_GPG11, 0x04},
};

irqreturn_t buttons_irq(int irq, void *dev_id)
{
  unsigned int pin_val;
  struct pin_desc *pin_desc = (struct pin_desc *)dev_id;
  pin_val = s3c2410_gpio_getpin(pin_desc->pin);
  if(pin_val)
  {
    keyVal = 0x80 | pin_desc->key_val;
  }
  else
  {
    keyVal = pin_desc->key_val;
  }
  wake_up_interruptible(&button_waitq);
  ev_press = 1;
  kill_fasync(&button_fasync, SIGIO, POLL_IN); 
  return IRQ_HANDLED;
}

int button_drv_open(struct inode *inode, struct file *file)
{
  int ret;
  if(file->f_flags&O_NONBLOCK)                               // 非阻塞
  {
    if(down_trylock(&button_lock))                // 获取信号量,失败返回非 0
    {
      printk("failed 1 button_drv_open \n");
      return -EBUSY;
    }
  }
  else                                 // 阻塞
  {
    down(&button_lock);                       // 获取信号量,如果无法获取则休眠
  }

  ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);
  if(ret<0)
  {
    printk("failed 1 button_drv_open");
  }
  ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[1]);
  if(ret<0)
  {
    printk("failed 2 button_drv_open");
  }
  ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[2]);
  if(ret<0)
  {
    printk("failed 3 button_drv_open");
  }
  ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[3]);
  if(ret<0)
  {
    printk("failed 4 button_drv_open");
  }
  return 0;
}

ssize_t button_drv_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
  int ret;

  if(file->f_flags&O_NONBLOCK)
  {
    if(ev_press!=1)                                                    // 没有按键按下直接返回
    {
      printk("failed 1 button_drv_read \n");
      return -EAGAIN;
    }
  }
  else                                                                  // 阻塞
  {
    wait_event_interruptible(button_waitq, ev_press);           // 如果按键没有动作则进入休眠
  }

  ret = copy_to_user(userbuf, &keyVal, 1);
  if(ret<0)
  {
    printk("failed 1 button_drv_read \n");
    return -1;
  }
  ev_press = 0;
  return 1;
}

int button_drv_close(struct inode *inode, struct file *file)
{
  free_irq(IRQ_EINT0, &pins_desc[0]); 
  free_irq(IRQ_EINT2, &pins_desc[1]);
  free_irq(IRQ_EINT11, &pins_desc[2]);
  free_irq(IRQ_EINT19, &pins_desc[3]);
  up(&button_lock);                                 // 释放互斥信号量
  return 0;
}

unsigned int button_drv_poll(struct file *file, poll_table *wait)
{
  unsigned int mask = 0;
  poll_wait(file, &button_waitq, wait);
  if(ev_press)
  {
    mask |= POLLIN | POLLRDNORM;
  }
  return mask;
}

int button_drv_fasync(int fd, struct file *file, int on)
{
  int ret;
  ret = fasync_helper(fd, file, on, &button_fasync);
  if(ret<0)
  {
    printk("failed 1 button_drv_fasync \n");
    return ret;
  }
  return 0;
}

struct file_operations button_drv_fops = {
  .owner = THIS_MODULE,
  .open = button_drv_open,
  .read = button_drv_read,
  .release = button_drv_close,
  .poll = button_drv_poll,
  .fasync = button_drv_fasync,
};

int __init button_drv_init(void)
{
  major = register_chrdev(0, DRIVER_NAME, &button_drv_fops);
  if(major<0)
  {
    printk("failed 1 button_drv_init \n");
  }
  button_class = class_create(THIS_MODULE, DEVICE_NAME);
  if(button_class<0)
  {
    printk("failed 2 button_drv_init \n");
  }
  button_class_device = class_device_create(button_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
  if(button_class_device<0)
  {
    printk("failed 3 button_drv_init \n");
  }
  gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  gpfdat = gpfcon + 1;
  gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  gpgdat = gpgcon + 1;
  return 0;
}

void __exit button_drv_exit(void)
{
  unregister_chrdev(major, DEVICE_NAME);
  class_device_unregister(button_class_device);
  class_destroy(button_class);
  iounmap(gpfcon);
  iounmap(gpgcon);
}

module_init(button_drv_init);
module_exit(button_drv_exit);

MODULE_LICENSE("GPL");

Makefile 文件:

obj-m += timer_drv.o

KERN_DIR = /work/system/linux-2.6.22.6

all:
make -C $(KERN_DIR) M=`pwd` modules 
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c

button_app_1.c 文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>

static int fd;

void button_signal(int signum)
{
  unsigned char keyVal;
  printf("signal = %d \n", signum);
  read(fd, &keyVal, 1);
  printf("keyVal = 0x%x \n", keyVal);
}

int main(int argc, char **argv)
{
  int oflags;
  char *filename;

  filename = argv[1];
  fd = open(filename, O_RDWR);         // 阻塞
  if(fd<0)
  {
    printf("can not open \n");
  }
  signal(SIGIO, button_signal); 
  fcntl(fd, F_SETOWN, getpid()); 
  oflags = fcntl(fd, F_GETFL); 
  fcntl(fd, F_SETFL, oflags|FASYNC); 
  while(1)
  {
    sleep(1000);
  }
  return 0;
}

button_app_2.c 文件:

 

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>

 

static int fd;

 

void button_signal(int signum)
{
  unsigned char keyVal;
  printf("signal = %d \n", signum);
  read(fd, &keyVal, 1);
  printf("keyVal = 0x%x \n", keyVal);
}

int main(int argc, char **argv)
{
  int oflags;
  char *filename;

  filename = argv[1];
  fd = open(filename, O_RDWR|O_NONBLOCK); // 非阻塞
  if(fd<0)
  {
    printf("can not open \n");
  }
  signal(SIGIO, button_signal); 
  fcntl(fd, F_SETOWN, getpid()); 
  oflags = fcntl(fd, F_GETFL); 
  fcntl(fd, F_SETFL, oflags|FASYNC);
  while(1)
  {
    sleep(1000);
  }
  return 0;
}

编译生成 button_drv.ko button_app_1、button_app_2 文件,运行./button_app_1 /dev/button_dev./button_app_2 /dev/button_dev

 

 

 

 

 

 

 

今天关于withnolock或的介绍到此结束,谢谢您的阅读,有关CSS中的display属性(none,block,inline,inline-block,inherit)、Entity Framework with NOLOCK、if(null check)-else vs try catch(NullPointerException)哪个更有效?、Linux 驱动 ——Button 驱动 6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞等更多相关知识的信息可以在本站进行查询。

本文标签: