Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import com.alesharik.webserver.logger.Logger;
- import com.mb3364.twitch.api.Twitch;
- import com.mb3364.twitch.api.handlers.ChannelResponseHandler;
- import com.mb3364.twitch.api.handlers.UserFollowsResponseHandler;
- import com.mb3364.twitch.api.models.Channel;
- import com.mb3364.twitch.api.models.UserFollow;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ForkJoinPool;
- public class Test {
- private volatile ArrayList<Twitch> twitch = new ArrayList<>();
- private ForkJoinPool pool;
- private ChannelHandler handler = new ChannelHandler();
- public void benchmark() {
- Logger.log("[test]", "Benchmark started!");
- pool.submit(new FollowersTask(twitch, "maks2103", 0));
- try {
- System.in.read();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void stop() {
- System.gc();
- }
- public void warm() {
- System.out.println("hi");
- pool = new ForkJoinPool(256);
- for(int i = 0; i < 256; i++) {
- twitch.add(new Twitch());
- }
- for(int i = 0; i < 256; i++) {
- pool.submit(new Thread() {
- @Override
- public void run() {
- try {
- Thread.sleep(1);
- System.out.println(getName());
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- class FollowersTask extends Thread {
- ArrayList<Twitch> twitches;
- String account;
- int par;
- public FollowersTask(ArrayList<Twitch> twitches, String account, int par) {
- this.twitches = twitches;
- this.par = par;
- this.account = account;
- }
- @Override
- public void run() {
- try {
- Twitch twitch = twitches.get(Integer.parseInt(getName().substring(8)));
- twitch.users().getFollows(account, new UserFollowsResponseHandler() {
- @Override
- public void onSuccess(int i, List<UserFollow> list) {
- list.forEach(channelFollow -> {
- Test.this.pool.submit(new Task(twitches, channelFollow.getChannel().getName(), Test.this.handler));
- if(par < 6) {
- Test.this.pool.submit(new FollowersTask(twitches, channelFollow.getChannel().getName(), ++par));
- }
- });
- }
- @Override
- public void onFailure(int i, String s, String s1) {
- System.out.println(i + s + s1);
- }
- @Override
- public void onFailure(Throwable throwable) {
- throwable.printStackTrace();
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- class Task extends Thread {
- ArrayList<Twitch> twitches;
- String account;
- ChannelHandler handler;
- public Task(ArrayList<Twitch> twitches, String account, ChannelHandler handler) {
- this.twitches = twitches;
- this.account = account;
- this.handler = handler;
- }
- @Override
- public void run() {
- Twitch twitch = twitches.get(Integer.parseInt(getName().substring(8)));
- twitch.channels().get(account, handler);
- }
- }
- class ChannelHandler implements ChannelResponseHandler {
- @Override
- public void onSuccess(Channel channel) {
- // Logger.log("[test]", channel.getName() + ":" + channel.getGame() + ":" + channel.getStatus());
- System.out.println(channel.getName());
- }
- @Override
- public void onFailure(int i, String s, String s1) {
- System.out.println(i + s + s1);
- }
- @Override
- public void onFailure(Throwable throwable) {
- throwable.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement