Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. Connection newConnection =new Connection(0);
  2. Connection newConnection1 =new Connection(1);
  3.  
  4. for(int i = 0; i < NE_list.getRowCount(); i=i+2)
  5. {
  6. if(NE_list.getValueAt(i, 0).toString().equals("true")) //Some condition here for the IP Address
  7. {
  8.  
  9. newConnection.i=i;
  10. newConnection1.i=i+1;
  11. newConnection.runprogram();
  12. newConnection1.runprogram();
  13. }
  14.  
  15.  
  16. }
  17.  
  18. class Connection extends Thread{
  19. int i;
  20. Connection(int val){
  21. i=val;
  22. }
  23. void runprogram(){
  24. start();
  25. }
  26. public void run(){
  27. //SNMP and FTP Code here for IP Address in index i of NE_list
  28. }
  29. }
  30.  
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.concurrent.ExecutorService;
  34. import java.util.concurrent.Executors;
  35.  
  36. class SomeRunnable implements Runnable {
  37. int threadNo = -1 ;
  38. List<String> list = new ArrayList<String>();
  39. public SomeRunnable(List list, int threadNo ) {
  40. this.list.addAll(list);
  41. this.threadNo =threadNo;
  42. }
  43. @Override
  44. public void run() {
  45. for (String element : list) {
  46. System.out.println("By Thread:" + threadNo+", Processed Element:" +element);
  47. }
  48. }
  49.  
  50. }
  51.  
  52. public class ExecutorDemo {
  53. public static void main(String[] args) {
  54. List<String> list = new ArrayList<String>();
  55. for (int i = 0; i < 100; i++) {
  56. list.add("Elem:"+i);
  57. }
  58. // Divide list
  59. int divideIndex = list.size()/2;
  60. //Create objects of Runnable
  61. SomeRunnable obj1 = new SomeRunnable(list.subList(0, divideIndex),1);
  62. SomeRunnable obj2 = new SomeRunnable(list.subList(divideIndex,list.size()),2);
  63.  
  64. //Create fixed Thread pool, here pool of 2 thread will created
  65. ExecutorService pool = Executors.newFixedThreadPool(2);
  66.  
  67. pool.execute(obj1);
  68. pool.execute(obj2);
  69.  
  70. pool.shutdown();
  71. }
  72.  
  73. }
  74.  
  75. class MyRunnable implements Runnable {
  76. List<List<String>> records;
  77. MyRunnable(List<List<String>> records){
  78. this.records = records;
  79. }
  80. public void run(){
  81. for(List list : records){
  82. System.out.println(Thread.currentThread().getName() + " : "+list.toString());
  83. }
  84. }}
  85.  
  86. public class FileProcessThreads {
  87. public List<List<String>> process(String fileName) throws IOException {
  88. List<List<String>> records = new ArrayList<>();
  89. BufferedReader br = new BufferedReader(new FileReader(fileName));
  90. String line = null;
  91. while((line = br.readLine()) != null){
  92. List<String> listValues = Arrays.asList(line.split(" "));
  93. records.add(listValues);
  94. }
  95. System.out.println(records.size());
  96. return records;
  97. }
  98. public static void main(String[] args) throws IOException {
  99. FileProcessThreads fp = new FileProcessThreads();
  100. List<List<String>> records = fp.process("test.txt");
  101. ExecutorService es = Executors.newFixedThreadPool(5);
  102. int recordsInEachThread = (int) (records.size()/5);
  103. System.out.println(recordsInEachThread);
  104.  
  105. MyRunnable my1 = new MyRunnable(records.subList(0, recordsInEachThread));
  106. MyRunnable my2 = new MyRunnable(records.subList(recordsInEachThread+1, recordsInEachThread*2));
  107. MyRunnable my3 = new MyRunnable(records.subList(recordsInEachThread*2 + 1, recordsInEachThread*3));
  108. MyRunnable my4 = new MyRunnable(records.subList(recordsInEachThread*3 + 1, recordsInEachThread*4));
  109. MyRunnable my5 = new MyRunnable(records.subList(recordsInEachThread*4 + 1, records.size() - 1));
  110. es.execute(my1);
  111. es.execute(my2);
  112. es.execute(my3);
  113. es.execute(my4);
  114. es.execute(my5);
  115. es.shutdown();
  116. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement