FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


JAVA queries





akshar
I have two questions

1. What is the scheduling policy for threads in java, who does the scheduling

2. What is the difference between notify and notifyAll functions in java for threads.
MrBlueSky
1. The Java VM does the scheduling, and how the thread is scheduled depends on the priority of the thread which is set with it's setPriority method:

Quote:

public final void setPriority(int newPriority)

Changes the priority of this thread.

First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.

Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group.

Parameters:
newPriority - priority to set this thread to


2. notify() picks one of the waiting threads at random and 'wakes' it. notifyAll() notifies all threads that are waiting on the object

Quote:

Thread scheduling is a gray area of Java programming because actual scheduling models are not defined by the Java specification. As a result, scheduling behavior can (and does) vary on different machines.

In a general sense, threads have a priority, and threads with a higher-priority tend to run more often that threads with a lower priority. The degree to which this is true depends on the underlying operating system; Windows operating systems give more precedence to the thread priority while Unix-style operating systems give more precedence to letting all threads have a significant amount of CPU time.


Source: Java Threads, Third Edition , Scott Oaks, Henry Wong
akshar
Can some one tell me
WHY JAVA does not support multiple inheritance?

But if we go to see all classes are inherited by Object class and hence any single inheritance is actually a multiple inheritance??
Isnt it?
Kelcey
akshar wrote:
Can some one tell me
WHY JAVA does not support multiple inheritance?

But if we go to see all classes are inherited by Object class and hence any single inheritance is actually a multiple inheritance??
Isnt it?



That's a good question... I haven't found a need for multiple inheritance (or situation). I'd like to know of some cases where it really helps. If I remember correctly, C++ has multiple inheritance. Something I miss sorely in java is overloaded operators Sad
MrBlueSky
akshar wrote:
Can some one tell me
WHY JAVA does not support multiple inheritance?



Java supports multiple inheritance through interfaces.

Quote:

Interfaces have another very important role in the Java programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. The Java programming language does not permit multiple inheritance (inheritance is discussed later in this lesson), but interfaces provide an alternative.
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

Source: http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html

Interfaces are a much cleaner way to implement multiple inheritance. That is way multiple inheritance in the C++ way is not very populair. C#, for example, also uses interfaces:

Quote:

This was a design decision by the C# language's lead architect (Anders Hejlsberg) to avoid complication, avoid "dependency hell," and simplify architectural requirements throughout CLI.
per_madsen_aalborg
Kelcey wrote:
I'd like to know of some cases where it really helps. If I remember correctly, C++ has multiple inheritance.


In the programming language Eiffel multiple inheritance is widely used. If you study the basic data structures of Eiffel you will find many examples of clever use of multiple inheritance.

Bertrand Meyer (the creator of Eiffel) recently wrote a column about the strengths of multiple inheritance.

The Truth about Multiple Inheritance
October 2006
Bertrand Meyer
http://www.eiffel.com/general/monthly_column/2006/October.html

Other good links about Eiffel:

http://www.eiffel.com
http://en.wikipedia.org/wiki/Eiffel_programming_language


Best Regards,

Per
MrBlueSky
per_madsen_aalborg wrote:

http://www.eiffel.com/general/monthly_column/2006/October.html


Quote:

Forcing an interface -- as used in multiple inheritance -- to be completely deferred means it can't have any algorithm at all. A typical case of where this rules out useful applications is a class like COMPARABLE, describing objects comparable through a total order relation. It seems at first like a good candidate for an interface: the feature "<" -- alias for `less_than' -- is deferred, to be implemented in descendants describing specific kinds of ordered values (integers, reals, tennis players, whatever). But now you will also want ">". There's only one reasonable definition for a > b once you have "<":

Result := (b < a)

This is an implementation! Same thing for "<=":

Result := (a < b) or (a.is_equal (b))

and ">=". So what you do in Eiffel is to make COMPARABLE deferred with the feature "<" deferred, and the other three effective (non-deferred, i.e. implemented) with the implementations as shown for two of them.

Then any descendant class that needs an ordering property, such as TENNIS_PLAYER, inherits COMPARABLE and implements "<" to describe the particular order relation of interest to you. The effective features -- here, the other three -- follow automatically. End of story. Thanks to multiple inheritance, TENNIS_PLAYER can inherit from anything else it wants to, for example a class PLAYER describing players in any game, or CLUB_MEMBER etc.

With interfaces you cannot do that. Any descendant of COMPARABLE will have to implement all the features. It's code duplication, with all the standard
problems: it's tedious to program; worse yet, it's error prone because there is no way to avoid a mistaken implementation of ">", "<=" or ">=" in some descendant.


He's got a very good point there. I try to think of a way to solve this kind of code-duplication using interfaces, but I don't see how..

It's a trade-off between implementation and creating a language 'easy' language on the one side and clean design on the other.
akshar
Multiple Interfaces can be inherited but that does not mean JAVA supports multiple inheritance bcause I am taking abt classes. All the functions in interfaces are either static of abstract so it does not help to implement multiple inheritance in any way.
Related topics
Java Game
Java
Add to favorites script - Java
Problems with Java
Java Programming Introductory
Language
DSP Tutorial with Java Applets
Programming links, info, and tutorials
Java
[JAVA TUTORIALS & FILES] - Java Scripting world
JAVA Forums
JAva HELP
SQL in java
Java hosting
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.