fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. public class main {
  8. public static void Main(String[] args) throws InterruptedException {
  9. Counter counter = new Counter();
  10. Thread threadA = new CounterThread(counter, 10);
  11. Thread threadB = new CounterThread(counter, 11);
  12. System.out.println("Starting A");
  13. threadA.start();
  14. System.out.println("Starting B");
  15. threadB.start();
  16. threadB.join();
  17. threadA.join();
  18. System.out.println("count: " + counter.count);
  19. }
  20. }
  21.  
  22. public class CounterThread extends Thread {
  23.  
  24. Counter counter;
  25. int n = 0;
  26.  
  27. public CounterThread(Counter counter, int n) {
  28. this.counter = counter;
  29. this.n = n;
  30. }
  31.  
  32. public void run() {
  33. for (int i = 0; i < n; i++) {
  34. counter.add(i);
  35. }
  36. }
  37. }
  38. public class Counter {
  39. int count = 0;
  40.  
  41. public synchronized void add(int value) {
  42. this.count += value;
  43. try {
  44. Thread.sleep(10);
  45. } catch (InterruptedException ex) {
  46. System.err.println("Should not get here!" + ex);
  47. }
  48. }
  49.  
  50. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class main is public, should be declared in a file named main.java
public class main {
       ^
Main.java:22: error: class CounterThread is public, should be declared in a file named CounterThread.java
public class CounterThread extends Thread {
       ^
Main.java:38: error: class Counter is public, should be declared in a file named Counter.java
public class Counter {
       ^
3 errors
stdout
Standard output is empty