美文网首页Kevin Learn Android
Kevin Learn Android:实现 Runnable

Kevin Learn Android:实现 Runnable

作者: Kevin_小飞象 | 来源:发表于2021-11-21 22:38 被阅读0次
    每日一图.jpg

    简介

    定义.png

    使用

    步骤.png

    实例

    需求一:实现 2 个窗口同时卖火车票;每个窗口卖 100 张,卖票速度都是 1 s/张

    1. 布局文件 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=".MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点击开始卖票" />
    </RelativeLayout>
    

    2. 逻辑代码:MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
    
        //主布局中定义了一个按钮用以启动线程
        Button button;
    
        //步骤1:创建线程类,实现Runnable接口
        private class MyThread1 implements Runnable{
    
            private int ticket = 100;//一个窗口有100张票
    
            //在run方法里复写需要进行的操作:卖票速度1s/张
            @Override
            public void run(){
                while (ticket>0){
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "卖掉了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:创建线程类的实例
                    //创建二个线程,模拟二个窗口卖票
                    MyThread1 mt1 = new MyThread1();
                    MyThread1 mt2 = new MyThread1();
    
                    Thread mt11 = new Thread(mt1, "窗口1");
                    Thread mt22 = new Thread(mt2, "窗口2");
    
                    //步骤3:调用start()方法开启线程
                    //启动二个线程,也即是窗口,开始卖票
                    mt11.start();
                    mt22.start();
                }
            });
        }
    }
    

    需求二:实现 2 个窗口同时卖火车票;每个窗口卖 100 张,但卖票速度不同:窗口 1 是 1 s/张,窗口 2 是 3 s/张
    逻辑代ma

    相关文章

      网友评论

        本文标题:Kevin Learn Android:实现 Runnable

        本文链接:https://www.haomeiwen.com/subject/lvhltrtx.html