Thread

pliz discirbe the concept of threading and packeage.
and also give me example

Posted Answers

Hi,
Please visit the link. There you will find almost all your JAVA related questions answered.
http://www.coders2020.com/java-interview-questions
However a sample example for creating threads is as follows:
It uses the Runnable interface to implement a thread (most preferable way to implement a thread)

  1. // Create multiple threads.
  2. class NewThread implements Runnable {
  3. String name; // name of thread
  4. Thread t;
  5.  
  6. NewThread(String threadname) {
  7. name = threadname;
  8. t = new Thread(this, name);
  9. System.out.println("New thread: " + t);
  10. t.start(); // Start the thread
  11. }
  12.  
  13. // This is the entry point for thread.
  14. public void run() {
  15. try {
  16. for(int i = 5; i > 0; i--) {
  17. System.out.println(name + ": " + i);
  18. Thread.sleep(1000);
  19. }
  20. } catch (InterruptedException e) {
  21. System.out.println(name + "Interrupted");
  22. }
  23. System.out.println(name + " exiting.");
  24. }
  25. }
  26.  
  27. class MultiThreadDemo {
  28. public static void main(String args[]) {
  29. new NewThread("One"); // start threads
  30. new NewThread("Two");
  31. new NewThread("Three");
  32.  
  33. try {
  34. // wait for other threads to end
  35. Thread.sleep(10000);
  36. } catch (InterruptedException e) {
  37. System.out.println("Main thread Interrupted");
  38. }
  39.  
  40. System.out.println("Main thread exiting.");
  41. }
  42. }


Answer by: indi.anupam
 
 

An example for package as follows..

  1. // A simple package
  2. package MyPack;
  3.  
  4. class Balance {
  5. String name;
  6. double bal;
  7.  
  8. Balance(String n, double b) {
  9. name = n;
  10. bal = b;
  11. }
  12.  
  13. void show() {
  14. if(bal<0)
  15. System.out.print("-->> ");
  16. System.out.println(name + ": $" + bal);
  17. }
  18. }
  19.  
  20. class AccountBalance {
  21. public static void main(String args[]) {
  22. Balance current[] = new Balance[3];
  23.  
  24. current[0] = new Balance("K. J. Fielding", 123.23);
  25. current[1] = new Balance("Will Tell", 157.02);
  26. current[2] = new Balance("Tom Jackson", -12.33);
  27.  
  28. for(int i=0; i<3; i++) current[i].show();
  29. }
  30. }

Another example describing the relation and access control between different packages...
  1. package p1;
  2.  
  3. public class Protection {
  4. int n = 1;
  5. private int n_pri = 2;
  6. protected int n_pro = 3;
  7. public int n_pub = 4;
  8.  
  9. public Protection() {
  10. System.out.println("base constructor");
  11. System.out.println("n = " + n);
  12. System.out.println("n_pri = " + n_pri);
  13. System.out.println("n_pro = " + n_pro);
  14. System.out.println("n_pub = " + n_pub);
  15. }
  16. }
  17.  
  18. class Derived extends Protection {
  19. Derived() {
  20. System.out.println("derived constructor");
  21. System.out.println("n = " + n);
  22.  
  23. // class only
  24. // System.out.println("n_pri = " + n_pri);
  25.  
  26. System.out.println("n_pro = " + n_pro);
  27. System.out.println("n_pub = " + n_pub);
  28. }
  29. }
  30.  
  31. class SamePackage {
  32. SamePackage() {
  33. Protection p = new Protection();
  34. System.out.println("same package constructor");
  35. System.out.println("n = " + p.n);
  36.  
  37. // class only
  38. // System.out.println("n_pri = " + p.n_pri);
  39.  
  40. System.out.println("n_pro = " + p.n_pro);
  41. System.out.println("n_pub = " + p.n_pub);
  42. }
  43. }

  1. package p2;
  2.  
  3. class Protection2 extends p1.Protection {
  4. Protection2() {
  5. System.out.println("derived other package constructor");
  6.  
  7. // class or package only
  8. // System.out.println("n = " + n);
  9.  
  10. // class only
  11. // System.out.println("n_pri = " + n_pri);
  12.  
  13. System.out.println("n_pro = " + n_pro);
  14. System.out.println("n_pub = " + n_pub);
  15. }
  16. }
  17.  
  18. class OtherPackage {
  19. OtherPackage() {
  20. p1.Protection p = new p1.Protection();
  21. System.out.println("other package constructor");
  22.  
  23. // class or package only
  24. // System.out.println("n = " + p.n);
  25.  
  26. // class only
  27. // System.out.println("n_pri = " + p.n_pri);
  28.  
  29. // class, subclass or package only
  30. // System.out.println("n_pro = " + p.n_pro);
  31.  
  32. System.out.println("n_pub = " + p.n_pub);
  33. }
  34. }

  1. // Demo package p1.
  2. package p1;
  3.  
  4. // Instantiate the various classes in p1.
  5. public class Demo {
  6. public static void main(String args[]) {
  7. Protection ob1 = new Protection();
  8. Derived ob2 = new Derived();
  9. SamePackage ob3 = new SamePackage();
  10. }
  11. }

  1. // Demo package p2.
  2. package p2;
  3.  
  4. // Instantiate the various classes in p2.
  5. public class Demo {
  6. public static void main(String args[]) {
  7. Protection2 ob1 = new Protection2();
  8. OtherPackage ob2 = new OtherPackage();
  9. }
  10. }

All the examples are from JAVA 2 Complete Reference by Herbert Schildt....


Answer by: indi.anupam
 
 

prateek, get back to us if u need something more.


Answer by: coders2020