Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 2.22 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java MultiThreading Code Error
  2. class ThreadDemo {
  3. public static void main(String args[]) {
  4.    try{
  5.  
  6.    int [] arr = new int[10] ;
  7.       for(int i = 0 ; i < 10 ; i++)
  8.          arr[i] = i ;
  9.  
  10.    for(int c =0 ; c < 2 ; c++){
  11.          for(int i = 0 ; i < 3 ; i++) //I want to run it on one thread
  12.              System.out.println(arr[i]);
  13.          for(int j = 0 ; j < 5 ; j++)  //I want to run it on another thread
  14.              System.out.println(arr[j]);
  15.             }
  16.  
  17. } catch (Exception e) {
  18.         System.err.println("Error: " + e.getMessage());
  19.     }
  20.    }
  21. }
  22.        
  23. class ThreadDemo {
  24. public static void main(String args[]) {
  25. try{
  26.  
  27. int [] arr = new int[10] ;
  28. for(int i = 0 ; i < 10 ; i++)
  29. arr[i] = i ;
  30.  
  31.     for(int c =0 ; c < 2 ; c++){
  32.          Thread thread1 = new Thread () {
  33.          public void run () {
  34.          for(int i = 0 ; i < 3 ; i++) //I want to run it on one thread
  35.            System.out.println(arr[i]);}
  36.           };
  37.  
  38.           Thread thread2 = new Thread () {
  39.           public void run () {
  40.           for(int j = 0 ; j < 5 ; j++) //I want to run it on one thread
  41.            System.out.println(arr[j]);}
  42.           };
  43.  
  44.  
  45.          }
  46.  
  47. } catch (Exception e) {
  48.         System.err.println("Error: " + e.getMessage());
  49.     }
  50.    }
  51. }
  52.        
  53. for (int c = 0; c < 2; c++) {
  54.     Thread thread1 = new Thread() {//<- here
  55.        
  56. final int[] arr = new int[10];
  57.        
  58. class MyThread extends Thread {
  59.     int[] array;
  60.     int iterations;
  61.  
  62.     public MyThread(int[] arr, int i) {
  63.         array=arr;
  64.         iterations = i;
  65.     }
  66.  
  67.     @Override
  68.     public void run() {
  69.         for (int i = 0; i < iterations; i++)
  70.             System.out.println(array[i]);
  71.     }
  72. }
  73.  
  74. class ThreadDemo {
  75.     public static void main(String args[]) {
  76.         try {
  77.  
  78.             int[] arr = new int[10];
  79.             for (int i = 0; i < 10; i++)
  80.                 arr[i] = i;
  81.  
  82.             for (int c = 0; c < 2; c++) {
  83.                 MyThread thread1 = new MyThread(arr, 3);
  84.                 MyThread thread2 = new MyThread(arr, 5);
  85.                 thread1.start();
  86.                 thread2.start();
  87.             }
  88.  
  89.         } catch (Exception e) {
  90.             System.err.println("Error: " + e.getMessage());
  91.         }
  92.     }
  93. }
  94.        
  95. Thread t = new Thread(new Runnable() {
  96.     public void run() {
  97.     }
  98. });
  99. t.start();