قله بلند
دوشنبه 07 دی 1388, 16:00 عصر
سلام
دو برنامه زیر، دقیقاً یک خروجی را تولید می کنند ولی با دو صورت متفاوت. در برنامه اول، تابع statr درون تابع main صدا زده می شود و سازنده نخ ها، فقط نام نخ ها را دریافت می کند ولی در برنامه دوم، سازنده علاوه بر دریافت نام نخ، آن را شروع نیز می کند یعنی تابع start را نیز فراخوانی می کند. ولی با وجود این تابع شروع کننده، هرگز نخی شروع نخواهد شد و دوباره باید از تابع start در main نیز استفاده کرد. چرا؟
import java.lang.Runnable;
class NewThread implements Runnable
{
String name; // name of thread
NewThread(String threadname)
{
name = threadname;
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin
{
public static void main(String args[])
{
Runnable r1 = new NewThread("One");
Thread ob1= new Thread(r1);
Runnable r2 = new NewThread("Two");
Thread ob2= new Thread(r2);
Runnable r3 = new NewThread("Three");
Thread ob3= new Thread(r3);
ob1.start();
ob2.start();
ob3.start();
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
try
{
System.out.println("Waiting for threads to finish.");
ob1.join();
ob2.join();
ob3.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
System.out.println("Main thread exiting.");
}
}
import java.lang.Runnable;
class NewThread implements Runnable
{
String name; // name of thread
Thread t;
NewThread(String threadname)
{
name = threadname;
t = new Thread(name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin3
{
public static void main(String args[])
{
Runnable r1 = new NewThread("One");
Thread ob1= new Thread(r1);
Runnable r2 = new NewThread("Two");
Thread ob2= new Thread(r2);
Runnable r3 = new NewThread("Three");
Thread ob3= new Thread(r3);
ob1.start();
ob2.start();
ob3.start();
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
try
{
System.out.println("Waiting for threads to finish.");
ob1.join();
ob2.join();
ob3.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
System.out.println("Main thread exiting.");
}
}
دو برنامه زیر، دقیقاً یک خروجی را تولید می کنند ولی با دو صورت متفاوت. در برنامه اول، تابع statr درون تابع main صدا زده می شود و سازنده نخ ها، فقط نام نخ ها را دریافت می کند ولی در برنامه دوم، سازنده علاوه بر دریافت نام نخ، آن را شروع نیز می کند یعنی تابع start را نیز فراخوانی می کند. ولی با وجود این تابع شروع کننده، هرگز نخی شروع نخواهد شد و دوباره باید از تابع start در main نیز استفاده کرد. چرا؟
import java.lang.Runnable;
class NewThread implements Runnable
{
String name; // name of thread
NewThread(String threadname)
{
name = threadname;
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin
{
public static void main(String args[])
{
Runnable r1 = new NewThread("One");
Thread ob1= new Thread(r1);
Runnable r2 = new NewThread("Two");
Thread ob2= new Thread(r2);
Runnable r3 = new NewThread("Three");
Thread ob3= new Thread(r3);
ob1.start();
ob2.start();
ob3.start();
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
try
{
System.out.println("Waiting for threads to finish.");
ob1.join();
ob2.join();
ob3.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
System.out.println("Main thread exiting.");
}
}
import java.lang.Runnable;
class NewThread implements Runnable
{
String name; // name of thread
Thread t;
NewThread(String threadname)
{
name = threadname;
t = new Thread(name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin3
{
public static void main(String args[])
{
Runnable r1 = new NewThread("One");
Thread ob1= new Thread(r1);
Runnable r2 = new NewThread("Two");
Thread ob2= new Thread(r2);
Runnable r3 = new NewThread("Three");
Thread ob3= new Thread(r3);
ob1.start();
ob2.start();
ob3.start();
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
try
{
System.out.println("Waiting for threads to finish.");
ob1.join();
ob2.join();
ob3.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.isAlive());
System.out.println("Thread Two is alive: "+ ob2.isAlive());
System.out.println("Thread Three is alive: "+ ob3.isAlive());
System.out.println("Main thread exiting.");
}
}