九草国产精品|极品熟妇喷水在线播放|亚洲无码卡通动漫亚洲一区|v视界制服诱惑一区三区

首頁

Android多線程:如何實現兩個相同的耗時任務

Android多線程 2018-05-30 4962

對于Android開發中多線程的應用,常見是多個任務的同時進行,那么這里舉一個例子:實現2個窗口同時賣火車票;每個窗口賣100張,賣票速度都是1s/張。具體該如何做呢?下面專業app開發報價燚軒科技來為大家講解一下。

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.carson_ho.demoforthread.MainActivity">

//設置一個按鈕用以啟動賣票
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點擊開始賣票"
        android:layout_centerInParent="true"
        />
</RelativeLayout>

MainActivity.java

package com.example.carson_ho.demoforthread;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    //主布局中定義了一個按鈕用以啟動線程
    Button button;

    //步驟1:創建線程類,繼承自Thread類
    private class MyThread extends Thread{

        private int ticket = 100;//一個窗口有100張票
        private String name; //窗口名, 也即是線程的名字

        public MyThread(String name){
            this.name=name;
        }

        //在run方法里復寫需要進行的操作
        @Override
        public void run(){
            while (ticket>0){
                ticket--;
                System.out.println(name + "賣掉了1張票,剩余票數為:"+ticket);

                try {
                    Thread.sleep(1000);//賣票速度是1s一張
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //Button按下時會開啟一個新線程執行賣票
            button = (Button) findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //步驟2:創建線程類的實例
                    //創建二個線程,模擬二個窗口賣票
                    MyThread mt1 = new MyThread("窗口1");
                    MyThread mt2 = new MyThread("窗口2");

                    //步驟3:調用start()方法開啟線程
                    //啟動二個線程,也即是窗口,開始賣票
                    mt1.start();
                    mt2.start();

                }
            });

        }
    }



測試結果如下:

好了,看到這里大家也都清楚運行的流程了,如果還存在疑問的話可以留言咨詢我們。

分享到微信朋友圈 +
打開微信,點擊底部的“發現”,使用 “掃一掃” 即可將網頁分享到我的朋友圈。 如何使用?
推薦文章

熱貼More +

服務范圍More +

聯系我們

請掃二維碼聯系客服

854221200@qq.com

185-3825-9583

QQ客服

關于  ·  招聘  ·  案例中心  ·  網站地圖

?@2018 燚軒科技版權所有 豫ICP備16015002號-4

百度提供搜索支持