本文的目的是介绍Ping服务的使用方法的详细情况,特别关注ping服务的使用方法有哪些的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解Ping服务的使用方法的机会,同
本文的目的是介绍Ping服务的使用方法的详细情况,特别关注ping服务的使用方法有哪些的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解Ping服务的使用方法的机会,同时也不会遗漏关于Angular2的管道Pipe的使用方法、C# using 的使用方法、countif函数的使用方法 PHP的可变变量名的使用方法分享、CyclicBarrier正确的使用方法和错误的使用方法的知识。
本文目录一览:- Ping服务的使用方法(ping服务的使用方法有哪些)
- Angular2的管道Pipe的使用方法
- C# using 的使用方法
- countif函数的使用方法 PHP的可变变量名的使用方法分享
- CyclicBarrier正确的使用方法和错误的使用方法
Ping服务的使用方法(ping服务的使用方法有哪些)
你可以采取手动通知和自动通知两种方式使用ping服务:
手动ping:访问http://ping.baidu.com/ping.html页面,在输入框中输入博客地址或者feed地址,点击“提交博客”按钮即可。
自动ping:如果您的博客程序支持自动ping功能,您只需把百度的Ping服务地址配置到你的Blog发布后台或者客户端程序中,就可以实现自动通知的功能。
百度ping服务的地址为:http://ping.baidu.com/ping/RPC2。
Angular2的管道Pipe的使用方法
管道Pipe可以将数据作为输入,然后按照规则将其转换并输出。在Angular2中有许多内置的Pipe,比如DatePipe、UpperCasePipe和CurrencyPipe等。在这里我们主要介绍如何自定义Pipe。
1. 管道定义
Pipe的定义如下代码所示:
import { PipeTransform, Pipe } from ''@angular/core''; /*users: Array<any> = [ { name: ''1'', id: 1 }, { name: ''2'', id: 2 }, { name: ''3'', id: 3 }, { name: ''4'', id: 4 }, { name: ''5'', id: 5 }, { name: ''6'', id: 6 } ];*/ @Pipe({ name: ''filterUser'' }) export class FilterUserPipe implements PipeTransform { transform(allUsers: Array<any>, ...args: any[]): any { return allUsers.filter(user => user.id > 3); } }
如代码所示,
- 需要使用@Pipe来装饰类
- 实现PipeTransform的transform方法,该方法接受一个输入值和一些可选参数
- 在@Pipe装饰器中指定管道的名字,这个名字就可以在模板中使用。
注意:当定义完成后,不要忘记在Module的declarations数组中包含这个管道。
2. 管道使用
user.template.html实现如下所示:
<div> <ul> <li *ngFor="let user of (users | filterUser)"> {{user.name}} </li> </ul> </div> <button (click)="addUser()"> add new user</button>
user.component.ts实现如下所示:
import { Component } from "@angular/core"; @Component({ templateUrl: ''./user.template.html'', }) export class EnvAppComponent { id = 7; users: Array<any> = [ { name: ''1'', id: 1 }, { name: ''2'', id: 2 }, { name: ''3'', id: 3 }, { name: ''4'', id: 4 }, { name: ''5'', id: 5 }, { name: ''6'', id: 6 } ]; addUser() { this.users.push({ name: this.id++, id: this.id++ }) } }
在user.component.ts中初始了数据users和定义一个添加user的方法,在user.template.html中使用自定义管道filterUser。
当启动应用时,可以发现只有id大于3的user被显示出来了。可是,当你点击按钮添加用户时,发现并没有什么反应,数据并没有改变。这是为什么呢?
3. 数据变更检测
在Angular2中管道分为两种:纯管道和非纯管道。默认情况下管道都是纯管道。
纯管道就是在Angular检测到它的输入值发生了纯变更时才会执行管道。纯变更也就是说原始数据类型(如String、Number和Boolean等)或者对象的引用发生变化。该管道会忽略对象内部的变化,在示例中,数组的引用没有发生改变,改变的只是数组内部的数据,所以当我们添加数据时并没有立即响应在页面上。
非纯管道会在组件的变更检测周期中执行,而且会对对象的内部数据进行检测。
在我们的示例中将管道变更为非纯管道是非常贱简单的,只要在管道元数据中将添加pure标志为false即可。
代码如下所示:
@Pipe({ name: ''filterUser'', pure: false }) export class FilterUserPipe implements PipeTransform { transform(allUsers: Array<any>, ...args: any[]): any { return allUsers.filter(user => user.id > 3); } }
这样每当我们添加新用户时,数据就会马上响应在页面上了。
在根模块声明的pipe在页面中引用有效,而在页面中引用的component中的模板则无效,这也是令我困惑的地方...
寻求了一些解决方案,大多是要注意得在根模块中声明,于是我做了个尝试,将组件也写成一个功能模块,并在组件功能模块中申明pipe,结果很欣喜,组件中的pipe生效了。
具体操作方法:
只需新建组件功能模块,并在改模块中申明pipe,myComponent.module.ts
import { NgModule } from ''@angular/core''; import { IonicPageModule } from ''ionic-angular''; import { myComponent } from ''myComponent.ts''; import { HelloPipe } from "hello.pipe.ts"; @NgModule({ declarations: [ myComponent, HelloPipe ], imports: [ IonicPageModule.forChild(myComponent) ], exports: [ myComponent ] }) export class MyComponent {}
大功告成,组件的模板引用pipe得以生效.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
- Angular2管道Pipe及自定义管道格式数据用法实例分析
- Angular管道PIPE的介绍与使用方法
C# using 的使用方法
1. using :对命名空间的引用
比如 using System; 这样的命名空间,在加入了dll 包之后,也要对包进行引用
对不同命名空间同一方法别名的区分即:定义别名
using System;
namespace someName1
{
public class some
{
public string getSomeString()
{
return "this is method of someName1";
}
}
}
namespace someName2
{
public class some
{
public string getSomeString()
{
return "this is method of someName2";
}
}
}
定义两个命名空间
2. using:定义别名
using oneName = someName1.some;
using twoName = someName2.some;
下面是使用
oneName one = new oneName();
Console.WriteLine( one.getSomeString());
twoName two = new twoName();
Console.WriteLine(two.getSomeString());
Console.Read();
作用:这样就避免了很多重名的麻烦,而且,使得有些很长的命名空间的名字的以简化
3. using:自动释放所新建的对象;
作用:① 自动释放,避免缓存,内存溢出
② 简化try catch 得到在此定义域内自动释放所新建的对象,以简化代码;
using (Class1 cls1 = new Class1(), cls2 = new Class1())
{
// the code using cls1, cls2
} // call the Dispose on cls1 and cls2
或
//自动释放所新建的二维码对象
using (MemoryStream ms = new MemoryStream())
{
qrCodeImage.Save(ms, ImageFormat.Jpeg);
returnImageData = ms.GetBuffer();
ms.Close();
}
参考文章:https://blog.csdn.net/echoerror/article/details/80907738
countif函数的使用方法 PHP的可变变量名的使用方法分享
通常变量通过下面这样的语句来命名 :
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
$a = ''hello'';
?>
可变变量名指的是使用一个变量的值作为这个变量的名称。在上面的例子中,通过使用两个$符号,你可以把hello设置成一个变量的名称,就像下面那样。
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
$$a = ''world'';
?>
通过上面的两个语句,有两个变量被定义:变量$a,装的内容是”hello” 以及变量$hello,装的内容是 “world”。 于是,下面的语言:
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
echo "$a ${$a}";
?>
跟下面的语句的输出完全一致:
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
echo "$a $hello";
?>
它们都输出:hello world。
为了使用数组的可变变量名,你需要解决一个歧义问题。就是,如果你写$$a[1],解析器需要明白究竟你的意思是要把$a[1]当成一个变量,还是要把$$a当成变量、[1]指的是这个变量的索引。解决这个歧义问题的语法是:第一种情况使用${$a[1]},第二种情况使用${$a}[1]。
类属性也可以通过可变属性名来访问。可变属性名从产生调用所在的变量的访问范围内获取。例如,如果你的表达式是这样的:$foo->$bar,那么运行时将会在本地变量范围内寻找变量$bar,它的值将会做为$foo对象的一个属性名。如果$bar是个数组也可以使用。
例1 可变变量名
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
class foo {
var $bar = ''I am bar.'';
}
$foo = new foo();
$bar = ''bar'';
$baz = array(''foo'', ''bar'', ''baz'', ''quux'');
echo $foo->$bar . "n";
echo $foo->$baz[1] . "n";
?>
上面的例子将会输出下面的结果:
I am bar.
I am bar.
警告
请注意,可变变量名不能用于PHP函数和类里的超级全局数组变量上。变量$this也是一个不能动态取名的特殊变量。
以上就介绍了countif函数的使用方法 PHP的可变变量名的使用方法分享,包括了countif函数的使用方法方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
CyclicBarrier正确的使用方法和错误的使用方法
CyclicBarrier是java推出的一个并发编程工具,它用在多个线程之间协同工作。线程约定到达某个点,到达这个点之后的线程都停下来,直到最后一个线程也到达了这个点之后,所有的线程才会得到释放。常用的场景是:多个worker线程,每个线程都在循环地做一部分工作,并在最后用cyclicBarrier.await()设下约定点,当最后一个线程做完了工作也到达约定点后,所有线程得到释放,开始下一轮工作。也就是下面这样:
1 while(!done()){
2 //working
3 cyclicBarrier.await();
4 }
CyclicBarrier还支持一个回调函数,每当一轮工作结束后,下一轮工作开始前,这个回调函数都会被调用一次。
但是,使用CyclicBarrier必须准守最佳实践的使用方法,否则,就可能达不到想要的效果。比如,下面这样,就是一种典型的错误使用方法:
private void process(CyclicBarrier cyclicBarrier) {
final int n = 100;
Runnable worker= new Runnable() {
@Override
public void run() {
try {
//模拟工作
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
cyclicBarrier.await();
} catch (BrokenBarrierException | InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("Worker is done");
System.out.println("Thread of Worker is "+ Thread.currentThread().getId());
};
for (int i = 0; i < n; i++) {
Thread t1 = new Thread(worker);
Thread t2 = new Thread(worker);
t1.start();
t2.start();
}
}
在上面的代码中,工作不在worker线程中循环,而是在开启工作的线程中循环,也就是说,它会不断地开启新的worker线程。这会导致的一个问题是,上一轮的回调还没执行完成,下一轮的工作就已经开始了。
那么为什么呢?下面来分析一下原因。
首先,要知道CyclicBarrier是如何做到在上一轮工作结束后下一轮工作开始前执行回调函数的。查看jdoc文档,里面有这么一句话“A CyclicBarrier supports an optional Runnable
command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. ”这是描述回调函数的,从描述中可以看到,回调函数是在最后一个线程到达约定点后,线程释放前被执行的。也就是说,回调函数的执行时间发生在下一轮工作前,这是通过在执行完回调函数再释放工作线程来实现的。
然后,我们再来看看上面错误的使用方法。在错误的使用方法中,主线程的每一轮循环中都开启了新的worker线程,这样在回调函数结束之前,前面开启的worker线程确实没有得到释放,但是,新开启的工作线程却完全可以执行下一轮工作,这就是为什么在回调函数执行完毕之前,新一轮的工作就已经开始了的原因。并且,错误方法中的每一个工作线程只执行一轮工作就结束了,每一轮工作之间的线程互不影响,这也就失去了协作性,因此,千万要避免写出这种代码。
关于CyclicBarrier使用的最佳时间,基本上就是官方示例中的用法了,如下:
1 class Solver {
2 final int N;
3 final float[][] data;
4 final CyclicBarrier barrier;
5
6 class Worker implements Runnable {
7 int myRow;
8 Worker(int row) { myRow = row; }
9 public void run() {
10 while (!done()) {
11 processRow(myRow);
12
13 try {
14 barrier.await();
15 } catch (InterruptedException ex) {
16 return;
17 } catch (BrokenBarrierException ex) {
18 return;
19 }
20 }
21 }
22 }
23
24 public Solver(float[][] matrix) {
25 data = matrix;
26 N = matrix.length;
27 barrier = new CyclicBarrier(N,
28 new Runnable() {
29 public void run() {
30 mergeRows(...);
31 }
32 });
33 for (int i = 0; i < N; ++i)
34 new Thread(new Worker(i)).start();
35
36 waitUntilDone();
37 }
38 }
最后在有一个问题是,回调函数是在哪一个线程里执行的?
根据我的demo测试发现,是在第一个到达的线程中执行的。当然,官方并没有明确规定这一点,也许以后会有变化吧,所以,我们也不能以来这一特征。我的demo如下:
public class Demo1 {
public static main(String[] args){
Demo1 demo = new Demo1();
demo1.showInfThreadWhenDirectly();
}
private void process(CyclicBarrier cyclicBarrier) {
final int n = 100;
Runnable worker= new Runnable() {
@Override
public void run() {
for (int i = 0; i < n; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
int arrival_index=cyclicBarrier.await();
if(0==arrival_index){
System.out.println("first arrival Thread in this iteration is: "
+Thread.currentThread().getId());
}
} catch (BrokenBarrierException | InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("Worker is done");
System.out.println("Thread of Worker is "+ Thread.currentThread().getId());
}
};
Thread t1 = new Thread(worker);
Thread t2 = new Thread(worker);
t1.start();
t2.start();
}
public void showInfThreadWhenDirectly(){
CyclicBarrier cyclicBarrier = new CyclicBarrier(2, () ->
System.out.println("[Directly] Thread in invert call function is"
+ Thread.currentThread().getId()));
process(cyclicBarrier);
System.out.println("[Directly] main Thread is "+ Thread.currentThread().getId());
}
}
输出结果如下:
[Directly] main Thread is 1
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is11
first arrival Thread in this iteration is: 11
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is11
first arrival Thread in this iteration is: 11
另外,官方还有一段:“
If the barrier action does not rely on the parties being suspended when it is executed, then any of the threads in the party could execute that action when it is released. To facilitate this, each invocation of
await()
returns the arrival index of that thread at the barrier. You can then choose which thread should execute the barrier action, for example:if (barrier.await() == 0) { // log the completion of this iteration }
”
意思是说,如果回调动作“arrier action”不需要在所有工作线程都停止的状态下执行的话,那么可以随便找一个工作线程去做这个动作。为了支持这个,CyclicBarrier 的await( )方法有一个返回值,返回的就是当前线程是第几个到达约定点(barrier)的。
参考https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html
我们今天的关于Ping服务的使用方法和ping服务的使用方法有哪些的分享已经告一段落,感谢您的关注,如果您想了解更多关于Angular2的管道Pipe的使用方法、C# using 的使用方法、countif函数的使用方法 PHP的可变变量名的使用方法分享、CyclicBarrier正确的使用方法和错误的使用方法的相关信息,请在本站查询。
本文标签: