Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*****************************MAIN ACTIVITY
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- private SeekBar horseBar1, horseBar2,horseBar3,horseBar4,horseBar5;
- private TextView txtRes;
- private String res="";
- private int place;
- public static ArrayList<String> listResult=new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- public void setPointer()
- {
- horseBar1=(SeekBar)findViewById(R.id.horse1);
- horseBar2=(SeekBar)findViewById(R.id.horse2);
- horseBar3=(SeekBar)findViewById(R.id.horse3);
- horseBar4=(SeekBar)findViewById(R.id.horse4);
- horseBar5=(SeekBar)findViewById(R.id.horse5);
- txtRes=(TextView)findViewById(R.id.txtRes);
- }
- public void startRace(View view)
- {
- final ThreadRace horse1=new ThreadRace(horseBar1,this);
- ThreadRace horse2=new ThreadRace(horseBar2,this);
- ThreadRace horse3=new ThreadRace(horseBar3,this);
- ThreadRace horse4=new ThreadRace(horseBar4,this);
- ThreadRace horse5=new ThreadRace(horseBar5,this);
- horse1.setName("Horse 1");
- horse2.setName("Horse 2");
- horse3.setName("Horse 3");
- horse4.setName("Horse 4");
- horse5.setName("Horse 5");
- horse1.start();
- horse2.start();
- horse3.start();
- horse4.start();
- horse5.start();
- }
- public void getResult(View view) {
- for (int counter=0;counter<listResult.size();counter++)
- {
- place=counter+1;
- res+="\n Place "+place+": "+listResult.get(counter);
- }
- txtRes.setText(res);
- listResult.clear();
- }
- }
- //***********************HORSE RACE CLASS
- import android.content.Context;
- import android.widget.SeekBar;
- import android.widget.Toast;
- import java.util.Random;
- public class ThreadRace extends Thread
- {
- private SeekBar horse;
- private Context context;
- private Random rnd=new Random();
- public ThreadRace(SeekBar horse, Context context)
- {
- this.horse=horse;
- this.context=context;
- }
- @Override
- public void run()
- {
- horse.setProgress(0);
- for (int counter=0;counter<=100;counter++)
- {
- horse.setProgress(counter);
- try
- {
- Thread.sleep(rnd.nextInt(100)+100);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- MainActivity.listResult.add(Thread.currentThread().getName());
- horse.post(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(context, MainActivity.listResult.get(MainActivity.listResult.size()-1)+" finished", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement