zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - 锁和并发处理: atomic 原子操作

    示例如下:

    /concurrent/AtomicDemo1.java

    /**
     * atomic 原子操作
     *
     * 在 java.util.concurrent.atomic 包内有很多支持多线程场景下的原子操作的类,比如 AtomicBoolean, AtomicInteger, AtomicLong, AtomicReference 等,下面只简单介绍用法,详细的看文档吧
     */
    
    package com.webabcd.androiddemo.concurrent;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.atomic.AtomicReference;
    
    public class AtomicDemo1 extends AppCompatActivity {
    
        private TextView _textView1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_concurrent_atomicdemo1);
    
            _textView1 = (TextView) findViewById(R.id.textView1);
    
            sample1();
            sample2();
        }
    
        // AtomicInteger - 支持原子操作的整型(懒得写多线程下的示例了,这里只演示简单用法)
        private void sample1() {
            AtomicInteger atomicInteger = new AtomicInteger(0);
            atomicInteger.set(100);
            int result1 = atomicInteger.getAndAdd(100);
            int result2 = atomicInteger.get();
    
            _textView1.append(String.format("%d, %d", result1, result2));
            _textView1.append("
    ");
        }
    
        // AtomicReference - 支持原子操作的引用类型(懒得写多线程下的示例了,这里只演示简单用法)
        private void sample2() {
            AtomicReference<String> atomicReference = new AtomicReference<String>();
            atomicReference.set("webabcd");
            String result = atomicReference.get();
    
            _textView1.append(result);
        }
    }
    
    

    /layout/activity_concurrent_atomicdemo1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    js 日期比较 (输入的是字符串格式)
    js 弹出确认 取消对话框
    存储过程中查询多个字段的值来判断
    SQL中IF查询
    SQL server 2000安装时提示我”以前的某个程序安装已在安装计算机上创建挂起的文件操
    怎样做人
    RSS
    Silverlight教程第四部分:使用 Style 元素更好地封装观感 (木野狐译)
    Silverlight 2 初览
    Silverlight教程第七部分: 使用控件模板定制控件的观感
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_concurrent_AtomicDemo1.html
Copyright © 2011-2022 走看看