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

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

    一手遮天 Android - 锁和并发处理: synchronized 锁方法

    示例如下:

    /concurrent/SynchronizedDemo1.java

    /**
     * synchronized 锁方法
     */
    
    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.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class SynchronizedDemo1 extends AppCompatActivity {
    
        private int _number = 0;
        private TextView _textView1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_concurrent_lockdemo1);
    
            _textView1 = (TextView) findViewById(R.id.textView1);
    
            sample();
        }
    
        // 启 2 个线程对 _number 不停的做 +1 的操作,有锁则结果正确,无锁则结果错误
        private void sample() {
            ExecutorService pool = Executors.newFixedThreadPool(2);
            pool.execute(new MyRunnable());
            pool.execute(new MyRunnable());
            pool.shutdown();
    
            while (true) {
                try {
                    boolean terminated = pool.awaitTermination(10, TimeUnit.MILLISECONDS);
                    // 线程池中的线程都执行完毕后则退出此循环
                    if (terminated) {
                        break;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            _textView1.setText(String.valueOf(_number));
        }
    
        private class MyRunnable implements Runnable {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    execTask();
                }
            }
        }
    
        // 锁方法
        private synchronized void execTask() {
            _number++;
        }
    }
    
    

    /layout/activity_concurrent_lockdemo1.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

  • 相关阅读:
    Maven安装及配置
    Java部分概念理解
    API.day01
    随机生成10元素数组并找出最大元素(Java)
    冒泡排序(Java)
    俄罗斯方块部分功能(Java)
    判断闰年(Java)
    判断质数(Java)
    基于DSP的IS95正向业务信道模块设计
    Lua程序设计(4th) 第一部分 语言基础
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_concurrent_SynchronizedDemo1.html
Copyright © 2011-2022 走看看