ورود

View Full Version : مبتدی: نخ در جاوا



قله بلند
جمعه 04 دی 1388, 13:29 عصر
با سلام
در برنامه زیر، به نظر هیچگاه استثنا رخ نخواهد داد. چه باید کرد تا استثنا رخ دهد و دستور درون catch اجرا شود؟
public class CurrentThreadDemo
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
System.out.println("CurrentThread: "+t);
//change current name of the thread
t.setName("My Thread");
System.out.println("After name change"+t);
try
{
for(int i=5;i>0;i--)
{
System.out.println(i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
}
}

قله بلند
جمعه 04 دی 1388, 17:41 عصر
سلام

یک سوال دیگه. در ابتدا یک نقل قول و سپس برنامه:
درون سازنده NewThread یک شیء Threadجدید با استفاده از دستور زیر ایجاد می شود:
t=new Thread(this,"Demo Thread");گذر دادن this به عنوان اولین آرگومان نشان می دهد که شما می خواهید نخ جدید تابع run() را روی شیء this فراخوانی نمایید.

ولی با وجود this خطا صادر می شود.
interface Runnable
{
public abstract void run();
}

class NewThread implements Runnable
{
Thread t;
NewThread()
{
t=new Thread("Demo Thread");
System.out.println("Child Thread: "+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println("Child Thread: "+i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Child Interrupted");
}
System.out.println("Exiting Child Thared");
}
}

public class ThreadDemo
{
public static void main(String args[])
{
NewThread t=new NewThread();
t.run();
try
{
for(int i=5;i>0;i--)
{
System.out.println("Main Thread: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Exiting Main Thared");
}
}خروجی باید اینگونه باشد ولی اینگونه نمی شود:
Main Thread:5
Child Thread:5
Child Thread:4
Main Thread:4
Child Thread:3
Child Thread:2
Main Thread:3
Child Thread:1
Exiting Child Thared
Main Thread:2
Main Thread:1
Exiting Main Thared

قله بلند
یک شنبه 06 دی 1388, 14:07 عصر
در پست قبلی، وقتی قسمت ابتدایی برنامه را اینگونه تغییر می دهم، دقیقاً خروجی مورد نظر پست قبل را تولید می کند.

class NewThread extends Thread
{
NewThread(){
start();}تحلیل چیست؟

قله بلند
یک شنبه 06 دی 1388, 14:11 عصر
با عرض سلام
تفاوت استفاده از Runnable و Thread در دو برنامه زیر چیست؟ چرا وقتی کلاس اصلی می خواهد Thread را گسترش دهد، نیازی به صدا زدن run نیست ولی برای پیاده سازی واسط Runnable باید این کار صورت پذیرد؟ یعنی پیاده سازی Runnable منجر به تولید نخ نمی شود؟
class Counter extends Thread {
/* This class simply runs a thread to count to 50 */
public int count = 1;
Counter()
{
start();
}
public void run() {
while (count < 50) {
try {
System.out.println(count);
sleep(500);
} catch (InterruptedException e) { }
count++;
}
}
}

public class B
{
public static void main(String args[])
{
Thread r = new Counter();
}
}


class Counter implements Runnable {
/* This class simply runs a thread to count to 50 */
public int count = 1;
public void run() {
while (count < 50) {
try {
System.out.println(count);
Thread.sleep(500);
} catch (InterruptedException e) { }
count++;
}
}
}

public class B
{
public static void main(String args[])
{
Runnable r = new Counter();
r.run();
}
}

قله بلند
جمعه 11 دی 1388, 17:17 عصر
چهار لینک زیر که در همین سایت جستجو کردم می تواند به بعضی از سوالات پیرامون نخ در جاوا پاسخ دهد:
http://barnamenevis.org/forum/showthread.php?t=123261
http://barnamenevis.org/forum/showthread.php?t=123116
http://barnamenevis.org/forum/showthread.php?t=125645
http://barnamenevis.org/forum/showthread.php?t=123547
و توضیحی بیشتر از http://www.go4expert.com/forums/showthread.php?t=4202
We can create a thread in Java by extending Thread Class. A better way to create a thread in Java is to implement Runnable interface. A thread can be created by extending Java Thread class also. Now the question arises why implementing Runnable interface is a better approach? Answer is, if the thread class you are creating is to be subclass of some other class, it can’t extend from the Thread class. This is because Java does not allow a class to inherit from more than one class. In such a case one can use Runnable interface to implement threads. یعنی اینکه در جاوا ارث بری از چند کلاس امکان پذیر نیست. اگر کلاسی داشته باشیم که خودش زیر کلاس کلاس دیگری باشد و بخواهیم این کلاس ما از کلاس Thread نیز ارث ببرد امکان پذیر نیست. در این حالت باید واسط Runnable را اجرا کنیم تا بتوانیم از نخ ها استفاده کنیم.