• 一手遮天 Android


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

    一手遮天 Android - 锁和并发处理: Lock 可 interrupt 的 Lock

    示例如下:

    /concurrent/LockDemo2.java

    /**
     * Lock(可 interrupt 的 Lock 的使用)
     *
     * Lock 是一个接口,ReentrantLock 实现了这个接口
     *     lockInterruptibly() - 在线程获取锁的过程中,如果线程被 interrupt() 则抛出 InterruptedException 异常
     */
    
    package com.webabcd.androiddemo.concurrent;
    
    import android.os.SystemClock;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class LockDemo2 extends AppCompatActivity {
    
        private TextView _textView1;
    
        private Lock _lock = new ReentrantLock();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_concurrent_lockdemo2);
    
            _textView1 = (TextView) findViewById(R.id.textView1);
    
            sample();
        }
    
        /**
         * 本例(使用 lockInterruptibly() 时)的运行结果类似如下:
         * ready(thread1)
         * running(thread1)
         * ready(thread2)
         * interrupted from thread(thread1)
         * interrupted from _lock(thread2)
         *
         * 本例(把 lockInterruptibly() 改成 lock() 时)的运行结果类似如下:
         * ready(thread1)
         * running(thread1)
         * ready(thread2)
         * interrupted from thread(thread1)
         * running(thread2)
         * interrupted from thread(thread2)
         */
        private void sample() {
            Thread thread1 = new Thread(new MyRunnable(), "thread1");
            thread1.setDaemon(true);
            thread1.start();
    
            Thread thread2 = new Thread(new MyRunnable(), "thread2");
            thread2.setDaemon(true);
            thread2.start();
    
            // 用于保证上面的 2 个线程都起来了
            SystemClock.sleep(1000);
    
            thread1.interrupt();
            thread2.interrupt();
        }
    
        class MyRunnable implements Runnable {
    
            @Override
            public void run() {
                try {
                    execTask();
                } catch (InterruptedException e) {
                    // 在通过 lockInterruptibly() 获取锁的过程中,线程被 interrupt() 了
                    writeMessage("interrupted from lock");
                }
            }
    
            private void execTask() throws InterruptedException {
                writeMessage("ready");
                _lock.lockInterruptibly(); // 可以把此处改成 _lock.lock(); 之后再运行一下,以便对比一下区别
                try {
                    writeMessage("running");
                    Thread.sleep(3000);
                    writeMessage("complete");
                } catch (InterruptedException e) {
                    writeMessage("interrupted from thread");
                } finally {
                    _lock.unlock();
                }
            }
        }
    
        private void writeMessage(final String message) {
            final String threadName = Thread.currentThread().getName();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    _textView1.append(String.format("%s(%s)
    ", message, threadName));
                }
            });
        }
    }
    
    

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

  • 相关阅读:
    (二)扩展原理:【2】BeanDefinitionRegistryPostProcessor
    寒假学习日报3
    寒假学习日报6
    寒假学习日报8
    寒假学习日报9
    寒假学习日报7
    寒假学习日报1
    寒假学习日报4
    构建之法阅读笔记1
    寒假学习日报2
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_concurrent_LockDemo2.html
Copyright © 2020-2023  润新知