Kotlin线程的简单使用

package com.example.admin.zkotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_photo.*


class MainActivity : AppCompatActivity() {
    var currentProgress =0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_photo)
        initThread();
    }

    private fun initThread() {
        Thread(Runnable {
            try {
                while (true) {
                    if (currentProgress >= 10) {
                        currentProgress = 0
                    }
                    Thread.sleep(1000)
                    currentProgress++;
                    Log.e("TAG","$currentProgress")
                    runOnUiThread { tv.text = currentProgress.toString() }
                }
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
        }).start()
    }

}

运行结果:

10-08 12:22:58.925 30730-30790/com.example.admin.zkotlin E/TAG: 1
10-08 12:22:59.926 30730-30790/com.example.admin.zkotlin E/TAG: 2
10-08 12:23:00.927 30730-30790/com.example.admin.zkotlin E/TAG: 3
10-08 12:23:01.928 30730-30790/com.example.admin.zkotlin E/TAG: 4
10-08 12:23:02.930 30730-30790/com.example.admin.zkotlin E/TAG: 5
10-08 12:23:03.935 30730-30790/com.example.admin.zkotlin E/TAG: 6
10-08 12:23:04.936 30730-30790/com.example.admin.zkotlin E/TAG: 7
10-08 12:23:05.938 30730-30790/com.example.admin.zkotlin E/TAG: 8
10-08 12:23:06.944 30730-30790/com.example.admin.zkotlin E/TAG: 9
10-08 12:23:07.952 30730-30790/com.example.admin.zkotlin E/TAG: 10
10-08 12:23:08.955 30730-30790/com.example.admin.zkotlin E/TAG: 1
10-08 12:23:09.965 30730-30790/com.example.admin.zkotlin E/TAG: 2

第二种实现方式,用handler消息处理  ,结果是一样的,贴一下代码

package com.example.admin.zkotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import kotlinx.android.synthetic.main.activity_photo.*


class MainActivity : AppCompatActivity() {
    var currentProgress = 0
    var num = 0

    private val mHandler = Handler(Handler.Callback { msg ->
        if (msg.what == 100) {
            num = msg.obj as Int
            Log.e("TAG", "$num")
            runOnUiThread { tv.text = num.toString() }
        }
        false
    })

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_photo)
        initThread();
    }

    private fun initThread() {
        Thread(Runnable {
            try {
                while (true) {
                    if (currentProgress >= 10) {
                        currentProgress = 0
                    }
                    Thread.sleep(1000)
                    currentProgress++;
                    val msg = Message()
                    msg.what = 100
                    msg.obj = currentProgress
                    mHandler.sendMessage(msg)
                }
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
        }).start()
    }

}

 

你可能感兴趣的:(kotlin学习)