Advertisement
Guest User

thread - race

a guest
May 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. RaceActivity.java
  2.  
  3. ---------------------------------------------------------------------
  4.  
  5. package com.example.teacher.threadproj;
  6.  
  7. import android.content.Context;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.widget.SeekBar;
  11.  
  12. import com.example.teacher.threadproj.threads.HorseRunnable;
  13.  
  14. public class RaceActivity extends AppCompatActivity {
  15.  
  16.     Context context;
  17.  
  18.     SeekBar seekHorse1;
  19.     SeekBar seekHorse2;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_race);
  25.  
  26.         setPointer();
  27.     }
  28.  
  29.     private void setPointer() {
  30.         context = this;
  31.  
  32.         seekHorse1 = findViewById(R.id.seek1);
  33.         seekHorse2 = findViewById(R.id.seek2);
  34.  
  35.         Thread horseThread1 = new Thread(new HorseRunnable(context,seekHorse1,1));
  36.         Thread horseThread2 = new Thread(new HorseRunnable(context,seekHorse2,2));
  37.  
  38.         horseThread1.start();
  39.         horseThread2.start();
  40.     }
  41. }
  42.  
  43.  
  44.  
  45. ------------------------------------------
  46.  
  47. HorseRunnable.java
  48. ------------------------------------------
  49.  
  50. package com.example.teacher.threadproj.threads;
  51.  
  52. import android.content.Context;
  53. import android.os.Handler;
  54. import android.widget.SeekBar;
  55. import android.widget.Toast;
  56.  
  57. import java.util.Random;
  58.  
  59. /**
  60.  * Created by teacher on 23/05/2018.
  61.  */
  62.  
  63. public class HorseRunnable implements Runnable {
  64.  
  65.     private Context context;
  66.     private SeekBar seekBar;
  67.     private int id;
  68.  
  69.     public HorseRunnable(Context context,SeekBar seekBar, int id){
  70.         this.context = context;
  71.         this.seekBar = seekBar;
  72.         this.id = id;
  73.  
  74.         this.seekBar.setProgress(0);
  75.         this.seekBar.setMax(15);
  76.     }
  77.  
  78.     @Override
  79.     public void run() {
  80.  
  81.         for(int i=0; i<=seekBar.getMax(); i+=1){
  82.             try {
  83.                 Thread.sleep(new Random().nextInt(500)+100);
  84.             } catch (InterruptedException e) {
  85.                 e.printStackTrace();
  86.             }
  87.             seekBar.setProgress(i);
  88.         }
  89.  
  90.         // android.os.Handler
  91.         Handler handler = new Handler(context.getMainLooper());
  92.         // handler can post code to execute on the ui thread. we get the ui thread by sending context.getMainLooper - the main (ui) thread
  93.  
  94.         // post notifies a thread that there is extra code to execute
  95.         handler.post(new Runnable() {
  96.             @Override
  97.             public void run() {
  98.                 Toast.makeText(context, "horse number "+id+" finished", Toast.LENGTH_SHORT).show();
  99.  
  100.             }
  101.         });
  102.  
  103.     }
  104. }
  105.  
  106.  
  107.  
  108. -----------------------------------------------
  109.  
  110.  
  111. activity_race.xml
  112. ------------------------------------------
  113.  
  114. <?xml version="1.0" encoding="utf-8"?>
  115. <LinearLayout android:layout_height="match_parent"
  116.     android:layout_width="match_parent"
  117.     android:orientation="vertical"
  118.     android:gravity="center"
  119.     xmlns:android="http://schemas.android.com/apk/res/android">
  120.  
  121.  
  122.     <TextView
  123.         android:layout_width="match_parent"
  124.         android:layout_height="wrap_content"
  125.         android:text="@string/myRaceTtl"
  126.         android:textAlignment="center"
  127.         android:textSize="@dimen/ttl"/>
  128.  
  129.     <LinearLayout
  130.         android:layout_width="match_parent"
  131.         android:layout_height="match_parent"
  132.         android:orientation="vertical"
  133.         android:layout_margin="@dimen/seekMargin"
  134.         >
  135.  
  136.  
  137.         <SeekBar
  138.             android:layout_weight="1"
  139.             android:id="@+id/seek1"
  140.             android:layout_width="match_parent"
  141.             android:layout_height="wrap_content"
  142.             android:progress="0"
  143.             android:max="100"
  144.             />
  145.  
  146.         <SeekBar
  147.             android:layout_weight="1"
  148.             android:id="@+id/seek2"
  149.             android:layout_width="match_parent"
  150.             android:layout_height="wrap_content"
  151.             android:progress="0"
  152.             android:max="100"
  153.             />
  154.  
  155.  
  156.  
  157.     </LinearLayout>
  158.  
  159. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement