是否适合使用AtomicReference.compareAndSet来设置对数据库调用结果的引用?(Is it appropriate to use AtomicReference.compareAndSet to set a reference to the results of a database call?)
我正在实现一个简单的缓存,缓存存储为AtomicReference。
private AtomicReference<Map<String, String>> cacheData;
应该从数据库表中填充(延迟)缓存对象。
我提供了一种方法来将缓存数据返回给调用者,但如果数据为空(即未加载),则代码需要从数据库加载数据。 为避免同步我想到使用compareAndSet()方法:
public Object getCacheData() { cacheData.compareAndSet(null, getDataFromDatabase()); // atomic reload only if data not set! return Collections.unmodifiableMap(cacheData.get()); }
以这种方式使用compareAndSet是否可以。 将数据库调用作为原子操作的一部分? 是否比同步方法更好/更差?
非常感谢任何建议..
I am implementing a simple cache with the cache stored as an AtomicReference.
private AtomicReference<Map<String, String>> cacheData;
The cache object should be populated (lazily) from a database table.
I provide a method to return the cache data to a caller, but if the data is null (ie. not loaded), then the code needs to load the data from the database. To avoid synchronized I thought of using the compareAndSet() method:
public Object getCacheData() { cacheData.compareAndSet(null, getDataFromDatabase()); // atomic reload only if data not set! return Collections.unmodifiableMap(cacheData.get()); }
Is it ok to use compareAndSet in this way ie. to involve a database call as part of the atomic action? Is it any better/worse than just synchronizing the method?
Many thanks for any advice..
原文:https://stackoverflow.com/questions/12992478
最满意答案
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="android.sample.MainActivity"> <TextView android:id="@+id/txtFont_size_hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sizes"/> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:weightSum="5"> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="5"> <TextView android:id="@+id/txtSize1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="@string/setting_font_text" android:textSize="15sp"/> <TextView android:id="@+id/txtSize_18" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="@string/setting_font_text" android:textSize="20sp"/> <TextView android:id="@+id/txtSize_24" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:gravity="center" android:text="@string/setting_font_text" android:textSize="25sp"/> <TextView android:id="@+id/textSize_30" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:gravity="center" android:text="@string/setting_font_text" android:textSize="30sp"/> <TextView android:id="@+id/txtSize_36" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:gravity="center" android:text="@string/setting_font_text" android:textSize="35sp"/> </LinearLayout> <SeekBar android:id="@+id/seekBarSetting_font_size" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="4"/>
现在我所做的是将Seekbar的宽度对齐到最后一个
TextView
的中心,在这种情况下是txtSize_36
并将ems设置为android:max="4"
所以有五个可能的值(你可以将其更改为你想要的很多)活动代码是:
public class MainActivity extends AppCompatActivity { SeekBar seekBar; private LinearLayout bar, ll; TextView txtSize_14, txtSize_36; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = (SeekBar) findViewById(R.id.seekBarSetting_font_size); txtSize_14 = (TextView) findViewById(R.id.txtSize1); txtSize_36 = (TextView) findViewById(R.id.txtSize_36); ll = (LinearLayout) findViewById(R.id.ll); } float density; @Override protected void onResume() { super.onResume(); ViewTreeObserver vto = ll.getViewTreeObserver(); //****old code (may not work on all orientations)**** /*vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { ll.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { ll.getViewTreeObserver().removeOnGlobalLayoutListener(this); } int width = ll.getMeasuredWidth(); int height = ll.getMeasuredHeight(); ViewGroup.LayoutParams params = seekBar.getLayoutParams(); density = getResources().getDisplayMetrics().density; int newwidth = (int) (txtSize_14.getLeft() / density); newwidth = newwidth+ (txtSize_36.getLeft() + txtSize_36.getRight()) / 2; params.width = newwidth; seekBar.setLayoutParams(params); } });*/ //****new code (should work on all orientations)**** vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { ll.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { ll.getViewTreeObserver().removeOnGlobalLayoutListener(this); } seekBar.setPadding(15, 0, 15, 0); seekBar.setX(((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) - 15); ViewGroup.LayoutParams params = seekBar.getLayoutParams(); int centerwidth = ((txtSize_36.getLeft() + txtSize_36.getRight()) / 2) - ((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) + 15; params.width = centerwidth; seekBar.setLayoutParams(params); } }); } }
layout for this :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="android.sample.MainActivity"> <TextView android:id="@+id/txtFont_size_hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sizes"/> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:weightSum="5"> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="5"> <TextView android:id="@+id/txtSize1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="@string/setting_font_text" android:textSize="15sp"/> <TextView android:id="@+id/txtSize_18" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="@string/setting_font_text" android:textSize="20sp"/> <TextView android:id="@+id/txtSize_24" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:gravity="center" android:text="@string/setting_font_text" android:textSize="25sp"/> <TextView android:id="@+id/textSize_30" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:gravity="center" android:text="@string/setting_font_text" android:textSize="30sp"/> <TextView android:id="@+id/txtSize_36" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:gravity="center" android:text="@string/setting_font_text" android:textSize="35sp"/> </LinearLayout> <SeekBar android:id="@+id/seekBarSetting_font_size" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="4"/>
Now what i have done is aligned the width of Seekbar till the center of the last
TextView
which in this case istxtSize_36
and have set the ems asandroid:max="4"
so there are five possible values(you can change this to as much as you want)Code for Activity is :
public class MainActivity extends AppCompatActivity { SeekBar seekBar; private LinearLayout bar, ll; TextView txtSize_14, txtSize_36; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = (SeekBar) findViewById(R.id.seekBarSetting_font_size); txtSize_14 = (TextView) findViewById(R.id.txtSize1); txtSize_36 = (TextView) findViewById(R.id.txtSize_36); ll = (LinearLayout) findViewById(R.id.ll); } float density; @Override protected void onResume() { super.onResume(); ViewTreeObserver vto = ll.getViewTreeObserver(); //****old code (may not work on all orientations)**** /*vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { ll.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { ll.getViewTreeObserver().removeOnGlobalLayoutListener(this); } int width = ll.getMeasuredWidth(); int height = ll.getMeasuredHeight(); ViewGroup.LayoutParams params = seekBar.getLayoutParams(); density = getResources().getDisplayMetrics().density; int newwidth = (int) (txtSize_14.getLeft() / density); newwidth = newwidth+ (txtSize_36.getLeft() + txtSize_36.getRight()) / 2; params.width = newwidth; seekBar.setLayoutParams(params); } });*/ //****new code (should work on all orientations)**** vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { ll.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { ll.getViewTreeObserver().removeOnGlobalLayoutListener(this); } seekBar.setPadding(15, 0, 15, 0); seekBar.setX(((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) - 15); ViewGroup.LayoutParams params = seekBar.getLayoutParams(); int centerwidth = ((txtSize_36.getLeft() + txtSize_36.getRight()) / 2) - ((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) + 15; params.width = centerwidth; seekBar.setLayoutParams(params); } }); } }
Here is screenshot for reference (I have clubbed for all positions of seekbar):
相关问答
更多-
不确定你是否想要白色但是可以尝试使用: button1.SetBackgroundColor(Color.argb(255, seekr, seekg, seekb)); button2.SetBackgroundColor(Color.argb(255, seekr, seekg, seekb)); Not sure if you are wanting white or not but could try using: button1.SetBackgroundColor(Color.argb(255 ...
-
我没有尝试过,但根据UIStackView文档,您应该将堆栈视图的对齐方式设置为.lastBaseline或.firstBaseline ,具体取决于您希望如何处理多行标签。 I haven‘t tried it, but according to the UIStackView documentation, you should set the stack view‘s alignment to either .lastBaseline or .firstBaseline, depending on ho ...
-
这显然会发生。 因为sb将始终是“最后”的SeekBar,并且rating将始终是“最后”评级TextView。 更改您的代码: private class MySimpleArrayAdapter extends ArrayAdapter
{ private final Context context; private final ArrayList> list; public MySimpleArrayAdapter(Context context ... -
最直接的方法可能是简单地在两者之间添加另一层。 您可以使用类似当前SeekBars的FrameLayout ,以便您拥有“胖乎乎的FrameLayout ”,然后在每个FrameLayout放置一个SeekBar。 The most straightforward way is probably to simply add another layer in between. You can use something like a FrameLayout where your current SeekBar ...
-
在dialog / Alertdialog中使用seekbars来更改活动中的设置(Use seekbars in dialog/Alertdialog to change settings in activity)[2022-02-17]
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null); 上面的代码必须是onClick事件的本地代码。 它应该是这样的: settings.setOnClickListener(new View.OnClick ... -
Cocos2d:如何使用不同的字体大小垂直对齐CCLabelTtf?(Cocos2d: How to Align CCLabelTtf vertically with different font size?)[2022-11-04]
使用cocos2d 2.x,使用以下命令: CCLabelTTF *label = [CCLabelTTF labelWithString:inText fontName:_defaultMenuItemFont fontSize:_defaultMenuItemFontSize ... -
让每个seekbar给你一个介于0和FF之间的字符串十六进制值,然后组装这些值并解析颜色: myColor = "#" + redSeekBarValue + greenSeekBarValue + blueSeekBarValue yourButtonHere.setBackgroundColor(Color.parseColor(myColor)); Have each seekbar give you a String hex value between 0 and FF, then assembl ...
-
而不是return 0; 通过return position; 它会工作 instead of return 0; pass return position; it will work
-
布局:似乎问题是我正在使用的Drawable正在课堂上缓存。 因此,SeekBar在最后一次更新时显示了Drawable的位置,这是最后一个SeekBar位置。 我认为这是我在本地缓存Drawable时误解的错误,而不是每个SeekBar使用不同的drawable。 关于为什么会出现这种情况的更好解释会帮助我很多,但至少它是固定的。 It appears the issue is that the Drawable I was using, was being cached in the class. Becau ...
相关文章
更多- Becoming a data scientist
- [转]So You Want To Be A Producer
- Solr: a custom Search RequestHandler
- HTML5 Web SQL Database 数据库
- Spring Data: a new perspective of data operations
- How to Start a Business in 10 Days
- Create a Bootable MicroSD Card
- Stack Overflow Architecture Update - Now At 95 Million Page Views A Month
- Spark - A Fault-Tolerant Abstraction for In-Memory Cluster Computing
- Hadoop源码分析之二(RPC机制之Call处理)
最新问答
更多- 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)