ThreadLocal使用全解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThreadLocal使用全解相关的知识,希望对你有一定的参考价值。

参考技术A

ThreadLocal,即线程变量,是一个以ThreadLocal对象为键,任意对象为值的存储结构。这个结构被附带在线程上,也就是说一个线程可以根据一个ThreadLocal对象查询到绑定在这个线程上的一个值。

每个线程拥有自己的局部变量毫无疑问比使用全局变量好,因为局部的变量只有县城自己能看见,而不会影响到其他线程。ThreadLocal本身能被多个线程共享使用,并且又能达到线程安全的目的。常用的就是get、set、initialValue等方法。

JDK建议将ThreadLocal定义为private static类型。
ThreadLocal最常用的地方就是为每个线程绑定一个数据连接、HTTP请求、用户身份信息等,这样一个线程的所有调用到的方法都可以非常方便地访问这些资源。

我们只需直接用new的方式创建ThreadLocal变量,并使用一个泛型参数指定ThreadLocal的类型。 如果ThreadLocal的初始值不被设定,它的默认初始值将被设定为null ,如果要更改初始值,我们通常有两种方法:

使用get和set方法即可

注意,在ThreadLocal的使用中,get到的值的来源有以下规律

ThreadLocal和Synchronized都是为了解决多线程中相同变量的访问冲突问题,不同的点是

正因为ThreadLocal的线程隔离特性,使他的应用场景相对来说更为特殊一些。在android中Looper、ActivityThread以及AMS中都用到了ThreadLocal。当某些数据是以线程为作用域并且不同线程具有不同的数据副本的时候,就可以考虑采用ThreadLocal。

UIAlertView使用全解

举例:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

技术分享
标准的双按钮,cancel那个buttonIndex 为0, ok button 的buttonIndex为1

 

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];
技术分享
和程序里的顺序一样,cancel   ok   thirdButton 的buttonIndex 分别为0 1 2


UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];
技术分享

同理,cancel   ok   thirdButton FourthButton的buttonIndex 分别为0 1 2 3

[alertView show];

 

UIAlertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
alertView--->这个不用多说了吧
buttonIndex---->从0开始
可以通过if (buttonIndex == 1) { } 这样的来控制点击了某个按钮需要做什么操作
 
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法在动画结束和视图隐藏之后调用
 
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法在动画开始和视图隐藏之前调用
 
- (void)alertViewCancel:(UIAlertView *)alertView
在视图将要被取消之前
例如,用户点击了home键
三个函数的调用顺序依次是:
alertViewCancel----》willDismissWithButtonIndex---》didDismissWithButtonIndex
 
- (BOOL)alertViewShouldEnableFirstOtherButton(UIAlertView *)alertView
ios 5+
设置yes / no  将会设置alertView 的第一个otherButton的enable属性
 
- (void)didPresentAlertView:(UIAlertView *)alertView
在视图提交给用户以后调用
 
-  (void)willPresentAlertView:(UIAlertView *)alertView
在视图提交给用户以前调用
 
这六个delegate 方法调用的顺序依次是
alertViewShouldEnableFirstOtherButton---->willPresentAlertView--->didPresentAlertView
---->clickedButtonAtIndex---->(如果会触发视图取消,则会调用alertViewCancel)willDismissWithButtonIndex---->didDismissWithButtonIndex
 
ios4.0以后 alertView不会自动随着程序转向后台而移除
alertView属性
1.alertViewStyle:
UIAlertViewStyleDefault 只弹信息和按钮
UIAlertViewStyleSecureTextInput 有一个textfield加密框
UIAlertViewStylePlainTextInput 有一个不加密的textfield
UIAlertViewStyleLoginAndPasswordInput 有两个textfield,Login和password
 
只要有textfield就可以用textfieldAtIndex来捕获并进行相应的操作例如换键盘类型
 
2.cancelButtonIndex
开始是0,如果没有设置cancel button 则是-1
 
3.delegate
如果没有设置则是nil
 
4.firstOtherButtonIndex
从0开始,如果没设置则是-1,而且没被设置则会被忽略
 
5.message 
消息
 
6.numberOfButtons
只读  alertView中的按钮数量
 
7.title
标题
 
8.visible
只读  如果是yes 表示被显示
 
实例方法
- (NSInteger)addButtonWithTitle:(NSString *)title
返回值是增加的Button的index
 
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
输入buttonIndex 返回button的标题
 
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
程序自动完成点击buttonIndex的button 并dismiss 整个alertView的操作
 
- (id)initWithTitle:(NSString *)title message:(NSString)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitles, ...
这个就不用多说了
 
- (void)show
要显示必须要调用这个alertview才会显示
 
-  (UITextField *)textfieldAtIndex:(NSInteger)textfieldIndex
返回值是textfield
UIAlertViewStyleDefault 没有
UIAlertViewStyleSecureInput textfieldIndex 只有一个为0
UIAlertViewStylePlainInput textfieldIndex 只有一个为0
UIAlertViewStyleLoginAndPasswordInput textfieldIndex有两个 0 1

以上是关于ThreadLocal使用全解的主要内容,如果未能解决你的问题,请参考以下文章

Android ThreadLocal类浅析

面试难题,不用ThreadLocal 程序会崩吗?

ThreadLocal

ThreadLocal(一)设计ThreadLocal的目的

数据库连接池为啥要用 ThreadLocal?

ThreadLocal 局部变量