本文将带您了解关于AutomaticReferenceCounting的新内容,另外,我们还将为您提供关于8PraticalExamplesofLinux“Touch”Command--referen
本文将带您了解关于Automatic Reference Counting的新内容,另外,我们还将为您提供关于8 Pratical Examples of Linux “Touch” Command--reference、asp.net-core – options的用途.AutomaticAuthenticate with UseJwtBearerAuthentication、AtomicReference、AtomicStampedReference 和 AtomicMarkableReference、AtomicReference、AtomicStampedReference、AtomicMarkableReference的实用信息。
本文目录一览:- Automatic Reference Counting
- 8 Pratical Examples of Linux “Touch” Command--reference
- asp.net-core – options的用途.AutomaticAuthenticate with UseJwtBearerAuthentication
- AtomicReference、AtomicStampedReference 和 AtomicMarkableReference
- AtomicReference、AtomicStampedReference、AtomicMarkableReference
Automatic Reference Counting
8 Pratical Examples of Linux “Touch” Command--reference
In Linux every single file is associated with timestamps,and every file stores the information of last access time,last modification time and last change time. So,whenever we create new file,access or modify an existing file,the timestamps of that file automatically updated.
In this article we will cover some useful practical examples of Linux touch command. Thetouch command is a standard program for Unix/Linux operating systems,that is used to create,change and modify timestamps of a file. Before heading up for touch command examples,please check out the following options.
Touch Command Options
- -a,change the access time only
- -c,if the file does not exist,do not create it
- -d,update the access and modification times
- -m,change the modification time only
- -r,use the access and modification times of file
- -t,creates a file using a specified time
1. How to Create an Empty File
The following touch command creates an empty (zero byte) new file called sheena.
# touch sheena
2. How to Create Multiple Files
By using touch command,you can also create more than one single file. For example the following command will create 3 files named, sheena, meena and leena.
# touch sheena meena leena
3. Change File’s Access Time using -a
We can change the access time of a file using -a option. By default it will take the current system time and update the atime field.
Before touch command is executed:
$ stat tgs.txtFile: `tgs.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 394283 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan)
Access: 2012-10-18 23:58:21.663514407 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-18 23:58:21.663514407 +0530
$ touch -a tgs.txt
After the above touch command (Please note that the access time is changed):
$ stat tgs.txtFile: `tgs.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 394283 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan)
Access: 2012-10-19 00:08:23.559514525 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-19 00:08:23.559514525 +0530
4. How to Avoid Creating New File
Using -c option with touch command avoids creating new files. For example the following command will not create a file called leena if it does not exists.
# touch -c leena
5. How to Change File Modification Time
If you like to change the only modification time of a file called leena,then use the -moption with touch command. Please note it will only updates the last modification times (not the access times) of the file.
# touch -m leena
6. Explicitly Setting Access and Modification time using -t and -d
Instead of taking the current time-stamp,you can explicitly specify the time using -t and -d options.
The format for specifying -t is [[CC]YY]MMDDhhmm[.SS]
$ touch -t [[CC]YY]MMDDhhmm[.SS]
The following explains the above format:
- CC – Specifies the first two digits of the year
- YY – Specifies the last two digits of the year. If the value of the YY is between 70 and 99,the value of the CC digits is assumed to be 19. If the value of the YY is between 00 and 37,the value of the CC digits is assumed to be 20. It is not possible to set the date beyond January 18,2038.
- MM – Specifies the month
- DD – Specifies the date
- hh – Specifies the hour
- mm – Specifies the minute
- SS – Specifies the seconds
For example:
$ touch -a -m -t 203801181205.09 tgs.txt
Verify the above change using stat command:
$ stat tgs.txt File: `tgs.txt' Size: 3 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 394283 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan) Access: 2038-01-18 12:05:09.000000000 +0530 Modify: 2038-01-18 12:05:09.000000000 +0530 Change: 2012-10-19 00:40:58.763514502 +0530
You can also use a string to change the time
Another example:
$ touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txt
For developers,touch command will be really helpful when you are working with
7. How to Use the time stamp of another File
The following touch command with -r option,will update the time-stamp of file leena with the time-stamp of meena file. So,both the file holds the same time stamp.
# touch -r leena meena
8. Create a File using a specified time
If you would like to create a file with specified time other than the current time,then the format should be.
# touch -t YYMMDDHHMM.SS tecmint
For example the below command touch command with -t option will gives the tecmint file a time stamp of 18:30:55 p.m. on December 10, 2012.
# touch -t 201212101830.55 tecmint
9. 批量创建文件
We’ve almost covered all the options available in the touch command for more options use “man touch“. If we’ve still missed any options and you would like to include in this list,please update us via comment Box.
reference :
http://www.tecmint.com/8-pratical-examples-of-linux-touch-command/
http://www.thegeekstuff.com/2012/11/linux-touch-command/
linux shell 脚本攻略
asp.net-core – options的用途.AutomaticAuthenticate with UseJwtBearerAuthentication
Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.IConvertible'.
到目前为止我可以看到的决定因素似乎是选项的设置.AutomaticAuthenticate.如果这是真的,那么我得到例外,否则,我没有.
什么是AutomaticAuthenticate,为什么我需要启用它?
app.UseJwtBearerAuthentication(options => { options.AutomaticAuthenticate = true; }
这是完整的堆栈跟踪:
at System.Convert.ToInt32(Object value,IFormatProvider provider) at System.IdentityModel.Tokens.Jwt.JwtPayload.GetIntClaim(String claimType) at System.IdentityModel.Tokens.Jwt.JwtPayload.get_Nbf() at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.Validatetoken(String token,TokenValidationParameters validationParameters,SecurityToken& validatedToken) at Microsoft.AspNet.Authentication.JwtBearer.JwtBearerHandler.<HandleAuthenticateAsync>d__1.MoveNext() --- End of stack trace from prevIoUs location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw() at Microsoft.AspNet.Authentication.JwtBearer.JwtBearerHandler.<HandleAuthenticateAsync>d__1.MoveNext() --- End of stack trace from prevIoUs location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.AspNet.Authentication.AuthenticationHandler`1.<InitializeAsync>d__48.MoveNext() --- End of stack trace from prevIoUs location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext() --- End of stack trace from prevIoUs location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Api.Startup.<<Configure>b__9_0>d.MoveNext() in ...\Startup.cs:line 156
更新根本原因
我们的代码库正在为nbf,exp和iat创建重复声明.这就解释了为什么get_Nbf在堆栈跟踪中以及关于“JArray”的抱怨,因为每个值都是数组而不是值.
解决方法
如果它没有发生,那么您需要通过在authorize属性中指定承载的方案来请求中间件设置标识.
[Authorize(AuthenticationSchemes = "YourBearerSchemeName")]
或者你在政策中设置这个;
options.AddPolicy("RequireBearer",policy => { policy.AuthenticationSchemes.Add("YourBearerSchemeName"); policy.RequireAuthenticatedUser(); });
因此,通过将其设置为false,您实际上并没有运行持有者的东西,直到您要求它为止,您只是将异常关闭直到稍后.
AtomicReference、AtomicStampedReference 和 AtomicMarkableReference
这三个都是自 JDK1.5 开始加入到 java.util.concurrent.atomic 下面的。他们都可以在 lock-free 的情况下以原子的方式更新对象引用。
一、AtomicReference
以原子方式更新对象引用。
static class User {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int age) {
this.age = age;
}
}
public static void main(String[] args) {
User user1 = new User(10);
User user2 = new User(20);
AtomicReference<User> atomicReference = new AtomicReference<>(user1);
System.out.println(atomicReference.get().getAge());
atomicReference.compareAndSet(user1, user2);
System.out.println(atomicReference.get().getAge());
}
二、AtomicStampedReference
解决了 AtomicReference 中 CAS 操作存在的 ABA 问题。
public static void main(String[] args) {
User user1 = new User(10);
User user2 = new User(20);
AtomicStampedReference<User> stampedReference = new AtomicStampedReference<>(user1, 1);
int[] stamp = new int[1];
// 获取引用对象和对应的版本号
System.out.println(stampedReference.get(stamp).getAge());
int oldStamp = stamp[0];
// 预期引用,新引用,预期版本号,新版本号
stampedReference.compareAndSet(user1, user2, oldStamp, 2);
System.out.println(stampedReference.get(stamp).getAge());
}
內部定义了一个 Pair 对象,相当于给引用加了一个版本号
public class AtomicStampedReference<V> {
private static class Pair<T> {
final T reference;
final int stamp;
private Pair(T reference, int stamp) {
this.reference = reference;
this.stamp = stamp;
}
static <T> Pair<T> of(T reference, int stamp) {
return new Pair<T>(reference, stamp);
}
}
private volatile Pair<V> pair;
public AtomicStampedReference(V initialRef, int initialStamp) {
pair = Pair.of(initialRef, initialStamp);
}
替换时的逻辑,当引用和版本号都相同时才使用 CAS 替换
public boolean compareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp) {
Pair<V> current = pair;
return expectedReference == current.reference &&
expectedStamp == current.stamp &&
((newReference == current.reference &&
newStamp == current.stamp) ||
casPair(current, Pair.of(newReference, newStamp)));
}
private boolean casPair(Pair<V> cmp, Pair<V> val) {
return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val);
}
三、AtomicMarkableReference
相对于 AtomicStampedReference,有时候,我们并不关心引用变量更改了几次,只是单纯的关心是否更改过
public static void main(String[] args) {
User user1 = new User(10);
User user2 = new User(20);
AtomicMarkableReference<User> stampedReference = new AtomicMarkableReference<>(user1, false);
boolean[] stamp = new boolean[1];
// 获取引用对象和对应的状态
System.out.println(stampedReference.get(stamp).getAge());
boolean oldStamp = stamp[0];
// 预期引用,新引用,预期状态,新状态
stampedReference.compareAndSet(user1, user2, oldStamp, false);
System.out.println(stampedReference.get(stamp).getAge());
}
内部和 AtomicStampedReference 一样
public class AtomicMarkableReference<V> {
private static class Pair<T> {
final T reference;
final boolean mark;
private Pair(T reference, boolean mark) {
this.reference = reference;
this.mark = mark;
}
static <T> Pair<T> of(T reference, boolean mark) {
return new Pair<T>(reference, mark);
}
}
private volatile Pair<V> pair;
public AtomicMarkableReference(V initialRef, boolean initialMark) {
pair = Pair.of(initialRef, initialMark);
}
public boolean compareAndSet(V expectedReference, V newReference, boolean expectedMark, boolean newMark) {
Pair<V> current = pair;
return
expectedReference == current.reference &&
expectedMark == current.mark &&
((newReference == current.reference &&
newMark == current.mark) ||
casPair(current, Pair.of(newReference, newMark)));
}
private boolean casPair(Pair<V> cmp, Pair<V> val) {
return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val);
}
https://segmentfault.com/a/1190000015831791
AtomicReference、AtomicStampedReference、AtomicMarkableReference
这三个都是自JDK1.5开始加入到java.util.concurrent.atomic
下面的。他们都可以在lock-free的情况下以原子的方式更新对象引用。
区别在于:AtomicStampedReference
内部维护了一个[reference, integer] pairs
的二元组。AtomicMarkableReference
内部维护了一个[reference, boolean] pairs
的二元组。
以下部分源码片段摘自JDK1.8(保留了注释):
- AtomicReference
public class AtomicReference<V> implements java.io.Serializable {
private static final long serialVersionUID = -1848883965231344442L;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
//class初始化时执行
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicReference.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
//通过volatile关键字保证value值的可见性。
private volatile V value;
public AtomicReference(V initialValue) {
value = initialValue;
}
public AtomicReference() {
}
//CAS操作
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(V expect, V update) {
return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}
}
- AtomicStampedReference
public class AtomicStampedReference<V> {
//内部维护一个静态内部类
//reference表示引用对象
//stamp表示时间戳(版本号),int型
private static class Pair<T> {
final T reference;
final int stamp;
private Pair(T reference, int stamp) {
this.reference = reference;
this.stamp = stamp;
}
static <T> Pair<T> of(T reference, int stamp) {
return new Pair<T>(reference, stamp);
}
}
private volatile Pair<V> pair;
/**
* Creates a new {@code AtomicStampedReference} with the given
* initial values.
*
* @param initialRef the initial reference
* @param initialStamp the initial stamp
*/
public AtomicStampedReference(V initialRef, int initialStamp) {
pair = Pair.of(initialRef, initialStamp);
}
/**
* Atomically sets the value of both the reference and stamp
* to the given update values if the
* current reference is {@code ==} to the expected reference
* and the current stamp is equal to the expected stamp.
*
* @param expectedReference the expected value of the reference
* @param newReference the new value for the reference
* @param expectedStamp the expected value of the stamp
* @param newStamp the new value for the stamp
* @return {@code true} if successful
*/
public boolean compareAndSet(V expectedReference,
V newReference,
int expectedStamp,
int newStamp) {
Pair<V> current = pair;
//通过增加了stamp(版本号)的CAS操作,可以避免ABA问题,即更新始终是递增的,不会出现往复。
return
expectedReference == current.reference &&
expectedStamp == current.stamp &&
((newReference == current.reference &&
newStamp == current.stamp) ||
casPair(current, Pair.of(newReference, newStamp)));
}
private boolean casPair(Pair<V> cmp, Pair<V> val) {
return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val);
}
}
- AtomicMarkableReference
public class AtomicMarkableReference<V> {
//这里几乎和AtomicStampedReference相同,只是从 final int stamp 变成了 final boolean mark
private static class Pair<T> {
final T reference;
final boolean mark;
private Pair(T reference, boolean mark) {
this.reference = reference;
this.mark = mark;
}
static <T> Pair<T> of(T reference, boolean mark) {
return new Pair<T>(reference, mark);
}
}
private volatile Pair<V> pair;
/**
* Creates a new {@code AtomicMarkableReference} with the given
* initial values.
*
* @param initialRef the initial reference
* @param initialMark the initial mark
*/
public AtomicMarkableReference(V initialRef, boolean initialMark) {
pair = Pair.of(initialRef, initialMark);
}
}
关于Automatic Reference Counting的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于8 Pratical Examples of Linux “Touch” Command--reference、asp.net-core – options的用途.AutomaticAuthenticate with UseJwtBearerAuthentication、AtomicReference、AtomicStampedReference 和 AtomicMarkableReference、AtomicReference、AtomicStampedReference、AtomicMarkableReference的相关信息,请在本站寻找。
本文标签: