Advertisement
Guest User

Test.java

a guest
Aug 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. package test;
  2.  
  3. import com.alesharik.webserver.logger.Logger;
  4. import com.mb3364.twitch.api.Twitch;
  5. import com.mb3364.twitch.api.handlers.ChannelResponseHandler;
  6. import com.mb3364.twitch.api.handlers.UserFollowsResponseHandler;
  7. import com.mb3364.twitch.api.models.Channel;
  8. import com.mb3364.twitch.api.models.UserFollow;
  9.  
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.concurrent.ForkJoinPool;
  14.  
  15. public class Test {
  16.     private volatile ArrayList<Twitch> twitch = new ArrayList<>();
  17.     private ForkJoinPool pool;
  18.     private ChannelHandler handler = new ChannelHandler();
  19.  
  20.     public void benchmark() {
  21.         Logger.log("[test]", "Benchmark started!");
  22.         pool.submit(new FollowersTask(twitch, "maks2103", 0));
  23.         try {
  24.             System.in.read();
  25.         } catch (IOException e) {
  26.             e.printStackTrace();
  27.         }
  28.     }
  29.  
  30.     public void stop() {
  31.         System.gc();
  32.     }
  33.  
  34.     public void warm() {
  35.         System.out.println("hi");
  36.         pool = new ForkJoinPool(256);
  37.         for(int i = 0; i < 256; i++) {
  38.             twitch.add(new Twitch());
  39.         }
  40.         for(int i = 0; i < 256; i++) {
  41.             pool.submit(new Thread() {
  42.                 @Override
  43.                 public void run() {
  44.                     try {
  45.                         Thread.sleep(1);
  46.                         System.out.println(getName());
  47.                     } catch (InterruptedException e) {
  48.                         e.printStackTrace();
  49.                     }
  50.                 }
  51.             });
  52.         }
  53.         try {
  54.             Thread.sleep(50);
  55.         } catch (InterruptedException e) {
  56.             e.printStackTrace();
  57.         }
  58.     }
  59.  
  60.     class FollowersTask extends Thread {
  61.         ArrayList<Twitch> twitches;
  62.         String account;
  63.         int par;
  64.  
  65.         public FollowersTask(ArrayList<Twitch> twitches, String account, int par) {
  66.             this.twitches = twitches;
  67.             this.par = par;
  68.             this.account = account;
  69.         }
  70.  
  71.         @Override
  72.         public void run() {
  73.             try {
  74.                 Twitch twitch = twitches.get(Integer.parseInt(getName().substring(8)));
  75.                 twitch.users().getFollows(account, new UserFollowsResponseHandler() {
  76.                     @Override
  77.                     public void onSuccess(int i, List<UserFollow> list) {
  78.                         list.forEach(channelFollow -> {
  79.                             Test.this.pool.submit(new Task(twitches, channelFollow.getChannel().getName(), Test.this.handler));
  80.                             if(par < 6) {
  81.                                 Test.this.pool.submit(new FollowersTask(twitches, channelFollow.getChannel().getName(), ++par));
  82.                             }
  83.                         });
  84.                     }
  85.  
  86.                     @Override
  87.                     public void onFailure(int i, String s, String s1) {
  88.                         System.out.println(i + s + s1);
  89.                     }
  90.  
  91.                     @Override
  92.                     public void onFailure(Throwable throwable) {
  93.                         throwable.printStackTrace();
  94.                     }
  95.                 });
  96.             } catch (Exception e) {
  97.                 e.printStackTrace();
  98.             }
  99.         }
  100.     }
  101.  
  102.     class Task extends Thread {
  103.         ArrayList<Twitch> twitches;
  104.         String account;
  105.         ChannelHandler handler;
  106.  
  107.         public Task(ArrayList<Twitch> twitches, String account, ChannelHandler handler) {
  108.             this.twitches = twitches;
  109.             this.account = account;
  110.             this.handler = handler;
  111.         }
  112.  
  113.         @Override
  114.         public void run() {
  115.             Twitch twitch = twitches.get(Integer.parseInt(getName().substring(8)));
  116.             twitch.channels().get(account, handler);
  117.         }
  118.     }
  119.  
  120.     class ChannelHandler implements ChannelResponseHandler {
  121.         @Override
  122.         public void onSuccess(Channel channel) {
  123. //            Logger.log("[test]", channel.getName() + ":" + channel.getGame() + ":" + channel.getStatus());
  124.             System.out.println(channel.getName());
  125.         }
  126.  
  127.         @Override
  128.         public void onFailure(int i, String s, String s1) {
  129.             System.out.println(i + s + s1);
  130.         }
  131.  
  132.         @Override
  133.         public void onFailure(Throwable throwable) {
  134.             throwable.printStackTrace();
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement