Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ThreadTest {
- static final T[] t = new T[100];
- static boolean m = false;
- public static void main(String[] args) throws InterruptedException {
- for (int i=0;i<100;i++) {
- t[i] = new T(i);
- }
- for (int i=0;i<100;i++) {
- t[i].start();
- }
- while (true) {
- for (int i = 0; i < 100; i++){System.out.print(t[i].x + ",");}
- System.out.println();
- try{Thread.sleep(1000);}catch(Exception e){}
- }
- }
- }
- class T extends Thread {int c,x;
- public T(int i){c=i;x=(int)(Math.random()*100);}
- public void run() {
- T a = ThreadTest.t[c-1 == -1 ? 99 : c-1];
- T b = ThreadTest.t[c+1 == 100 ? 0 : c+1];
- while (true) {
- if (ThreadTest.m)
- if (a.x < x && b.x < x)
- x--;
- else if (a.x > x && b.x > x)
- x++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement