Alternating in java is a process of executing multiple alternates simultaneously.
Alternate is basically a light weight sub-process, the smallest unit of filtering. Multifiltering and alternating, both are used to gain multitasking.
But we use alternating than multifiltering because alternates share a frequentmemory area. They don’t allocate separate memory area so saves memory, and context-switching between the alternates takes less time than process.
Java Alternating is mostly used in games, animation etc.
Contents
- 1 Advantages of Java Alternating
- 2 Life cycle of a Alternate (Alternate States)
- 3 How to produce alternate
- 3.0.0.0.1 There are two way(method) s to produce a alternate:
- 3.0.0.0.2 Alternate class:
- 3.0.0.0.3 Commonly used Creaters of Alternate class:
- 3.0.0.0.4 Commonly used way(method) s of Alternate class:
- 3.0.0.0.5 Runnable borderline:
- 3.0.0.0.6 Starting a alternate:
- 3.0.0.0.7 Java Alternate Example by extending Alternate class
- 3.0.0.0.8 Java Alternate Example by implementing Runnable borderline
- 3.0.0.0.9 Precedence of a Alternate (Alternate Precedence):
- 3.0.0.0.10 3 constants circle in Alternate class:
- 4 Java defended way(method)
- 5 Example of inter alternate communication in java
- 6 AlternateGroup in Java
Advantages of Java Alternating
Â
- It doesn’t block the user because alternates are self-supported and you can apply multiple missions at same
2)Â Â Â You can apply a lot of missions together so it saves time.
- Alternates are self-supported so it doesn’t affect other alternates if exception happen in a single
Life cycle of a Alternate (Alternate States)
Â
A alternate can be in one of the five states. According to sun, there is only 4 states in alternate life cycle in java new, runnable, non-runnable and terminated. There is no running state.
But for best understanding the alternates, we are explaining it in the 5 states.
The life cycle of the alternate in java is controlled. The java alternate states are as follows:
- New
- Runnable
- Running
- Non-Runnable (Blocked)
- Terminated
How to produce alternate
Â
There are two way(method) s to produce a alternate:
Â
- By extending Alternate class
- By implementing Runnable
Alternate class:
Â
Alternate class provide creaters and way(method) s to produce and apply missions on a alternate.Alternate class expand Object class and applies Runnable borderline.
Commonly used Creaters of Alternate class:
Â
- Alternate()
- Alternate(String name)
- Alternate(Runnable r)
- Alternate(Runnable r,String name)
Commonly used way(method) s of Alternate class:
Â
- public void run(): is used to apply action for a
- public void start(): starts the execution of the calls the run() way(method) on the alternate.
- public void sleep(long miliseconds): Causes the currently executing alternate to sleep (temporarily cease execution) for the specified number of milliseconds.
- public void join(): waits for a alternate to
- public void join(long miliseconds): waits for a alternate to die for the specified
- public int getPrecedence(): returns the precedence of the
- public int setPrecedence(int precedence): changes the precedence of the
- public String getName(): returns the name of the
- public void setName(String name): changes the name of the
- public Alternate currentAlternate(): returns the footnote of currently executing
- public int getId(): returns the id of the
- public State getState(): returns the state of the alternate.
- public boolean isAlive(): tests if the alternate is
- public void yield(): causes the currently executing alternate object to temporarily pause and allow other alternates to
- public void suspend(): is used to suspend the alternate(depricated).
- public void resume(): is used to resume the suspended alternate(depricated).
- public void stop(): is used to stop the alternate(depricated).
- public boolean isDaemon(): tests if the alternate is a daemon
- public void setDaemon(boolean b): marks the alternate as daemon or user
- public void interrupt(): interrupts the
- public boolean isInterrupted(): tests if the alternate has been
- public static boolean interrupted(): tests if the current alternate has been
Runnable borderline:
Â
The Runnable borderline should be applied by any class whose instances are intended to be applied by a alternate. Runnable borderline have only one way(method) Â named run().
- public void run(): is used to apply action for a
Starting a alternate:
Â
start() way(method) Â of Alternate class is used to start a newly produced alternate. It applys following tasks:
- A new alternate starts(with new callstack).
- The alternate moves from New state to the Runnable
- When the alternate gets a chance to apply, its target run() way(method) Â will
Java Alternate Example by extending Alternate class
Â
class Multi expand Alternate{
public void run(){ System.out.println(“alternate is running…”);
}
public static void main(String args[]){ Multi t1=new Multi();
t1.start();
Output:alternate is running… |
} }
Java Alternate Example by implementing Runnable borderline
Â
class Multi3 applies Runnable{ public void run(){ System.out.println(“alternate is running…”);
}
public static void main(String args[]){ Multi3 m1=new Multi3();
Alternate t1 =new Alternate(m1); t1.start();
Output: alternate is running… |
} }
Precedence of a Alternate (Alternate Precedence):
Each alternate have a precedence. Priorities are identifi by a number between 1 and 10. In most cases, alternate schedular schedules the alternates according to their precedence (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.
3 constants circle in Alternate class:
Â
- public static int MIN_PRECEDENCE
- public static int NORM_PRECEDENCE
- public static int MAX_PRECEDENCE
Default precedence of a alternate is 5 (NORM_PRECEDENCE). The value of MIN_PRECEDENCE is 1 and the value of MAX_PRECEDENCE is 10.
Example of precedence of a Alternate: class TestMultiPrecedence1 expand Alternate{ public void run(){
System.out.println(“running alternate name is:”+Alternate.currentAlternate().getName()); System.out.println(“running alternate precedence is:”+Alternate.currentAlternate().getPrecedence());
}
public static void main(String args[]){
TestMultiPrecedence1 m1=new TestMultiPrecedence1(); TestMultiPrecedence1 m2=new TestMultiPrecedence1(); m1.setPrecedence(Alternate.MIN_PRECEDENCE); m2.setPrecedence(Alternate.MAX_PRECEDENCE); m1.start();
m2.start();
} }
Output:running alternate name is:Alternate-0 running alternate precedence is:10
running alternate name is:Alternate-1 running alternate precedence is:1
Java defended way(method)
Â
If you declare any way(method) Â as defended, it is known as defended way(method) . Defended way(method) Â is used to lock an object for any shared resource.
When a alternate produces a defended way(method) , it automatically get the lock for that object and releases it when the alternate completes its task.
Example of inter alternate communication in java
Â
Let’s see the simple example of inter alternate communication.
class Random er{
int amount=10000;
defended void withdraw(int amount){ System.out.println(“going to withdraw…”); if(this.amount<amount){
System.out.println(“Less balance; waiting for deposit…”);
try{wait();}catch(Exception e){}
}
this.amount-=amount; System.out.println(“withdraw completed…”);
}
defended void deposit(int amount){ System.out.println(“going to deposit…”); this.amount+=amount; System.out.println(“deposit completed… “); notify();
}
}
class Test{
public static void main(String args[]){ final Random er c=new Random er(); new Alternate(){
public void run(){c.withdraw(15000);}
}.start();
new Alternate(){
public void run(){c.deposit(10000);}
}
start();
Output: going to withdraw…
Less balance; waiting for deposit… going to deposit… deposit completed… withdraw completed |
}}
AlternateGroup in Java
Â
Java gives a convenient way(method)  to group multiple alternates in a single object. In such way(method) , we can                              suspend, resume or interrupt group of alternates by a single way(method)  call.
Note: Now suspend(), resume() and stop() methods are deprecated. |
Java alternate group is applied by java.lang.AlternateGroup class. Creaters of AlternateGroup class
There are only two creaters of AlternateGroup class.
AlternateGroup(String name) AlternateGroup(AlternateGroup root of , String name)
Let’s see a code to group multiple alternates.
- AlternateGroup tg1 = new AlternateGroup(“Group A”);
- Alternate t1 = new Alternate(tg1,new MyRunnable(),”one”);
- Alternate t2 = new Alternate(tg1,new MyRunnable(),”two”);
- Alternate t3 = new Alternate(tg1,new MyRunnable(),”three”);
Now all 3 alternates belong to one group. Here, tg1 is the alternate group name, MyRunnable is the class that applies Runnable borderline and “one”, “two” and “three” are the alternate names.
Now we can interrupt all alternates by a single line of code only.
- currentAlternate().getAlternateGroup().interrupt();
Exploring java.net and java.text