Programmer Coding

Alternating in java

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.

 

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:

  1. New
  2. Runnable
  3. Running
  4. Non-Runnable (Blocked)
  5. Terminated

 

How to produce alternate

 

There are two way(method) s to produce a alternate:

 

  1. By extending Alternate class
  2. 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:

 

  1. Alternate()
  2. Alternate(String name)
  3. Alternate(Runnable r)
  4. Alternate(Runnable r,String name)

 

Commonly used way(method) s of Alternate class:

 

  1. public void run(): is used to apply action for a
  2. public void start(): starts the execution of the calls the run() way(method) on the alternate.
  3. public void sleep(long miliseconds): Causes the currently executing alternate to sleep (temporarily cease execution) for the specified number of milliseconds.
  4. public void join(): waits for a alternate to
  5. public void join(long miliseconds): waits for a alternate to die for the specified
  6. public int getPrecedence(): returns the precedence of the
  7. public int setPrecedence(int precedence): changes the precedence of the
  8. public String getName(): returns the name of the
  9. public void setName(String name): changes the name of the
  10. public Alternate currentAlternate(): returns the footnote of currently executing
  11. public int getId(): returns the id of the
  12. public State getState(): returns the state of the alternate.
  13. public boolean isAlive(): tests if the alternate is
  14. public void yield(): causes the currently executing alternate object to temporarily pause and allow other alternates to
  15. public void suspend(): is used to suspend the alternate(depricated).
  16. public void resume(): is used to resume the suspended alternate(depricated).
  17. public void stop(): is used to stop the alternate(depricated).
  18. public boolean isDaemon(): tests if the alternate is a daemon
  19. public void setDaemon(boolean b): marks the alternate as daemon or user
  20. public void interrupt(): interrupts the
  21. public boolean isInterrupted(): tests if the alternate has been
  22. 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().

  1. 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:

  1. A new alternate starts(with new callstack).
  2. The alternate moves from New state to the Runnable
  3. 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:

 

  1. public static int MIN_PRECEDENCE
  2. public static int NORM_PRECEDENCE
  3. 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.

 

  1. AlternateGroup tg1 = new AlternateGroup(“Group A”);
  2. Alternate t1 = new Alternate(tg1,new MyRunnable(),”one”);
  3. Alternate t2 = new Alternate(tg1,new MyRunnable(),”two”);
  4. 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.

 

  1. currentAlternate().getAlternateGroup().interrupt();
Exploring java.net and java.text

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top