[Java lista] szálkezelés?

Zoltan Mozes Zoltan_Mozes at epam.com
2007. Jan. 30., K, 11:17:29 CET


Egy teljesen ismeretlen (bár angol) könyvet szeretnék a téma iránt érdeklődők figyelmébe ajánlani:


Complete Java® 2
Certification: Study Guide,
Fifth Edition
Philip Heller
Simon Roberts
SYBEX®


<OFF>
	Integer sybxAdvCount = Sybex.getAdvertisementCount();
	AssertEquals(sybxAdvCount, Integer.MAX_VALUE);
</OFF>


"
This section has presented two strategies for constructing threads: extending
Thread and
implementing Runnable. Superficially, the only difference between these two strategies is the
location of the run() method. The second strategy, where a runnable target is passed into the
constructor, is perhaps a bit more complicated in the case of the simple examples we have considered.
However, there are good reasons why you might choose to make this extra effort. The
run() method, like any other member method, is allowed to access the private data, and call the
private methods, of the class of which it is a member. Putting run() in a subclass of Thread may
mean that the method cannot access the features it needs (or cannot access those features in a
clean, reasonable manner).
Another reason that might persuade you to implement your threads using runnables rather
than subclassing Thread is the single-implementation inheritance rule. If you write a subclass of
Thread, it cannot be a subclass of anything else; but using Runnable, you can subclass whatever
other parent class you choose.
Finally, from an object-oriented point of view, a subclass of Thread combines two unrelated
functionalities: support for multithreading inherited from the Thread superclass and execution
behavior provided by the run() method. These functionalities are not closely related, so good
object-oriented discipline suggests that they exist in two separate classes. In the jargon of objectoriented
analysis, if you create a class that extends Thread, you’re saying that your class “is a”
thread. If you create a class that implements Runnable, you’re saying that your class “is associated
with” a thread.
Thread Fundamentals 217
To summarize, you can use two approaches to specify which run() method will be executed
by a thread:
 Subclass Thread. Define your run() method in the subclass.
 Write a class that implements Runnable. Define your run() method in that class. Pass an
instance of that class into your call to the Thread constructor.
"




Best Regards,
Zoltán Mózes


EPAM Systems
Budapest office, Hungary (GMT+1)

Office phone:        +36 (1) 327-74-XX
Office fax:            +36 (1) 327-74-20
Mobile phone:       +36 (20) XXX-XXX
E-mail:                 Zoltan_Mozes at epam.com

http://www.epam.com

CONFIDENTIALITY CAUTION AND DISCLAIMER
This message is intended only for the use of the individual(s) or entity(ies) to which it is addressed and contains information that is legally privileged and confidential. If you are not the intended recipient, or the person responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. All unintended recipients are obliged to delete this message and destroy any printed copies.

-----Original Message-----
From: javalist-bounces at javagrund.hu [mailto:javalist-bounces at javagrund.hu] On Behalf Of Jozsef Tabanyi
Sent: Tuesday, January 30, 2007 9:57 AM
To: java list
Subject: Re: [Java lista] szálkezelés?

Szija,

> Van-e valami más ok amiatt akkor is jobban járok ha implementálom a
> Runnable-t, minthogy örököljem a Thread osztályt?
Mivel a Java-ban nincs tobbszoros orokles, ezert ha az osztalyod a
Thread osztalybol szarmazik, akkor mar nem tud masbol.
Viszont a feladat gyakran megkoveteli, hogy masbol szarmaztasd az
osztalyod. Szoval szerintem inkabb ne szarmaztass a Thread-bol,
hanem ha szukseged van egy uj szalra, akkor az egy peldanyvaltozo
legyen, es a befogado osztaly meg implementalja a Runnable interfeszt.

Lehetne meg vaganykodni nevtelen belso osztalyokkal,
de szerintem te meg nem tartassz ott.

> Sok féle példát mutat a szállak leállítására.
A szalak leallitasanak egyetlen civilizalt modja, ha a vezerles
elhagyja a run() metodust. Tehat ha a run()-ban porog egy while
ciklus, akkor arrol kell gondoskodni, hogy ez valahogy leallithato
legyen. Az teljesen mindegy, hogy mi az a logikai feltetel, hogy
egy sima boolean valtozo vagy a Thread objektum referencioa null-ra
lett-e allitva. Ez a te szabad dontesed.

Es vegul egy egyszeru pelda:

public final class Engine implements Runnable {

	private Thread thread;

	/**
	 *
	 */
	public Engine() {
		super();
		// TODO Auto-generated constructor stub
	}


	public void start() {
		if(thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	}

	public void run() {
		// TODO Auto-generated method stub
		while(thread != null) {

			//do something here

			try {
				Thread.sleep(100);//lets run other threads
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				stop();
				break;
			}
		}
	}

	public void stop() {
		thread = null;
	}
}


joco

Jozsef Tabanyi
mailto:jozsef at tabanyi.hu
http://www.tabanyi.hu
On Tue, 30 Jan 2007, [iso-8859-2] Maglódszki Viktor wrote:

> Sziasztok !
>
> Már nem is tudom, hogy hová soroljam a levelet :) Legyen szálkezelés az egyszerûség kedvéért.
> Szeretném kihangsúlyozni, hogy a tegnapi nap elolvastam a kék Java könyv elsõ  kötetében a 15ös fejezetet "Párhuzamosság, szálkezelés".
> Már-már csak félve teszem fel kérdéseimet, amik némi zavarodásból adódnak, az itt és a könyvben olvasottakból.
> Igyekszem szépen érthetõen, pontokban összefoglalva irni ja és írásjeleket is igyekszem használni :)
>
> 1.
> Thread osztály öröklése és a Runnable interfész implementálása témakör.
> A könyv nagyon sok példában a Thread-et örökölteti, persze megemlíti (és igen értem is), hogy ott a Runnable is.
> Ha az osztályunk már öröklõdik akkor muszály az implementálás->ok ezt értem.
> Kérdésem:
> Van-e valami más ok amiatt akkor is jobban járok ha implementálom a Runnable-t, minthogy örököljem a Thread osztályt?
>
> 2.
> Sok féle példát mutat a szállak leállítására.
> Van az alap logikai változó
> while (isRunning()) .... féle megoldás running = false ha meg akarom állitani a szállat kivûlrõl egy synchronized stop metódusban
> a könyv szerint: "...gondoskodni kell egy volatile módosítással ellátott Thread típusú refernciáról ami a futó szálra mutat."
> private volatile boolean isRunning = true; (már tudom a volatile hatását is)
> ezt a példát egy olyan osztályon mutatja ami kiterjeszti a Thread-et
> "...a Runnable interfész esetén szükséges egy Thread, vagy annak egy alosztályára utaló referenciát definiálnunk..."
>
> private volatile Thread runningId = this;
> public void run() {
>  while (runningId == this) {
>  ...
>  }
> }
> stop() {
>  runningId = null;
> }
>
> viszont amiben ezt a példát mutatja az is extends Thread
> bár simán lehet, hogy csak nem furcsa
> azt mondja a könyv, hogy a Runnable-nél kell ilyen, erre bemutatja egy olyan osztályon ami extends Thread
> kérdésem: van-e valami számottevõ különbség a kettõ között?
>
> elõre is köszönöm!
>
> üdv, MV

_______________________________________________
Javalist mailing list
Javalist at javagrund.hu
http://javagrund.hu/mailman/listinfo/javalist


További információk a(z) Javalist levelezőlistáról