Minggu, 15 April 2012


Contoh source code proses computer : 

import java.util.Random;

public class ShortestJobFirst {              
    static class Process {
        public int arrivalTime;
        public int startTime;
        public int burstTime;
        public Process(int t) { burstTime=t; }
    }
    static class Queue {
        class QueueNode {
            Process  key;
            QueueNode next = null;
            QueueNode(Process p, QueueNode n) { key=p; next=n; }
            QueueNode(Process p) { key=p; }
        }
        QueueNode tail;
        public  Queue() {
            tail = null;
        }
        public  boolean  isEmpty() {
            return tail==null;
        }
        public  void  enqueue(Process x, float burstTime) {
            if ( tail==null ) {
                tail = new QueueNode(x); tail.next=tail;
            }
            else tail = (tail.next = new QueueNode(x,tail.next));
        }
        public Process dequeue() {
            if ( tail==null ) return null;
            else {  QueueNode p = tail.next;
            if ( p!=tail ) tail.next = p.next;
            else tail = null;
            return  p.key;
            }
        }
    }

    public static void main(String[]  args) {
        int  T = 0;                    
        int  N = 0;                    
        int  NumProcesses =  100;       
        int  QueuedProcesses = 0;       
        int  totalProcessDuration = 0;
        int totalTurnaroundTime = 0;    
        Process  activeProcess = null;
        int  lastProcessArrival = 0;  
        Random rand0 = new Random();   
        Random rand1 = new Random();   
        Random rand2 = new Random();   
        Queue q = new Queue();
      

        while ( N < NumProcesses ) {
            int newProcessArrival = rand0.nextInt(1801);
            do {
                u = 1.-rand1.nextDouble();
                v = 1.-rand2.nextDouble();
                x = Math.sqrt (-2.*Math.log(u))*Math.cos(2.*Math.PI*v) + 2.0;  /
            } while ( x<0 );
            int newProcessDuration = (int) Math.round (x*1000);
            // Create the process p
            Process p = new Process(newProcessDuration);
            lastProcessArrival += newProcessArrival;
            if ( lastProcessArrival < T ) lastProcessArrival = T;
            p.arrivalTime = lastProcessArrival;
          
            // If the active process has finished, start the next one
            if ( activeProcess == null ) {
                activeProcess = p;
                T = p.startTime = p.arrivalTime;
            }
            // Otherwise, put the process in the queue
            else {  q.enqueue(p, p.burstTime);   QueuedProcesses++; }
            do { // this terminates processes
                if ( T + activeProcess.burstTime > lastProcessArrival ) break;
                totalProcessDuration += activeProcess.burstTime;
                T += activeProcess.burstTime;
                int  turnaroundTime = T - activeProcess.arrivalTime;
                totalTurnaroundTime  +=  turnaroundTime;
                System.out.printf ("Process %d terminated. Turnaround time: %.2f milliseconds.\n",
                        ++N, (double) turnaroundTime/1000);
                System.out.printf (" \t(arrival %.2f,  begin %.2f,  end %.2f,  duration %.2f)\n",
                        (double) activeProcess.arrivalTime/1000, (double) activeProcess.startTime/1000,
                        (double) (activeProcess.startTime+activeProcess.burstTime)/1000,
                        (double) activeProcess.burstTime/1000);
                // If terminated processes >= to max processes, break;
                if ( N >= NumProcesses ) break;
                if ( q.isEmpty( ) ) {  activeProcess = null;   break; }
                // Otherwise, dequeue the active process and start the next one
                else {
                    activeProcess = q.dequeue( );  QueuedProcesses--;
                    if ( activeProcess.arrivalTime <= T ) activeProcess.startTime = T;
                    else activeProcess.startTime = T = activeProcess.arrivalTime;
                }
            } while(true);
        }   //  end while loop

        // Print the results
        System.out.printf ("\nSimulation time: %.2f milliseconds\n", (double) T/1000);
        System.out.printf ("Average turnaround time: %.2f milliseconds\n",
                (double) totalTurnaroundTime/NumProcesses/1000);
        System.out.printf ("Average process duration: %.2f milliseconds\n",
                (double) totalProcessDuration/NumProcesses/1000);
        System.out.printf ("Throughput: %.2f processes per second\n", (double) NumProcesses/T * 1000000);
        System.out.println ("Processes in the queue at the end of the simulation: "  + QueuedProcesses);
    }   //  end main
}





Gambar hasil output proses : 





First In First Out

Source code : 

package fifo;

import java.util.*;

public class FIFO{
  public static void main(String[] args) {
  Stack stack=new Stack();
  stack.push(new Integer(10));
  stack.push("a");
  System.out.println("The contents of Stack is" + stack);
  System.out.println("The size of an Stack is" + stack.size());
  System.out.println("The number poped out is" + stack.pop());
  System.out.println("The number poped out is " + stack.pop());
  //System.out.println("The number poped out is" + stack.pop());
  System.out.println("The contents of stack is" + stack);
  System.out.println("The size of an stack is" + stack.size());
  }
}

Gambar output proses : 



Thread

  • Definisi: 
Ulir atau thread (singkatan dari "thread of execution") dalam ilmu komputer, diartikan sebagai sekumpulan perintah (instruksi) yang dapat dilaksanakan (dieksekusi) secara sejajar dengan ulir lainnya, dengan menggunakan cara time slice (ketika satu CPU melakukan perpindahan antara satu ulir ke ulir lainnya) atau multiprocess (ketika ulir-ulir tersebut dilaksanakan oleh CPU yang berbeda dalam satu sistem). Ulir sebenarnya mirip dengan proses, tapi cara berbagi sumber daya antara proses dengan ulir sangat berbeda. Multiplethread dapat dilaksanakan secara sejajar pada sistem komputer. Secara umum multithreadingmelakukan time-slicing (sama dengan time-division multipleks), di mana sebuah CPU bekerja pada ulir yang berbeda, di mana suatu kasus ditangani tidak sepenuhnya secara serempak, untuk CPU tunggal pada dasarnya benar-benar melakukan sebuah pekerjaan pada satu waktu. Teknik penggantian (switching) ini memungkinkan CPU seolah-olah bekerja secara serempak.

  • Contoh source code thread pada Java:

public class csj01x3 extends Thread
{
   public static void main(String args[]) throws Throwable
   {
      new csj01x3().start();
      new csj01x3().start();
      // main thread is ending here,
      // Thread-0 and Thread-1 continue to run.
   }

   public void run()
   {
      try {
         for (int i=0; i<100; i++) {
            System.out.println("thread "
               +Thread.currentThread().getName()+" step "+i);
            Thread.sleep(500);
         }
      } catch (Throwable t) { }
   }
}


Senin, 02 April 2012


Program 1
//Find Maximum of 2 nos.
class Maxof2{
  public static void main(String args[]){
      //taking value as command line argument.
      //Converting String format to Integer value
      int i = Integer.parseInt(args[0]);
      int j = Integer.parseInt(args[1]);
      if(i > j)
          System.out.println(i+" is greater than "+j);
      else
          System.out.println(j+" is greater than "+i);
  }
}

Program 2
//Find Minimum of 2 nos. using conditional operator
class Minof2{
      public static void main(String args[]){
      //taking value as command line argument.
      //Converting String format to Integer value
      int i = Integer.parseInt(args[0]);
      int j = Integer.parseInt(args[1]);
      int result = (i<j)?i:j;
      System.out.println(result+" is a minimum value");
  }
}

Program 3
/* Write a program that will read a float type value from the   keyboard and print the following output.
   ->Small Integer not less than the number.
   ->Given Number.
   ->Largest Integer not greater than the number.
*/
class ValueFormat{
  public static void main(String args[]){
      double i = 34.32; //given number 
      System.out.println("Small Integer not greater than the number : "+Math.ceil(i));
      System.out.println("Given Number : "+i);
      System.out.println("Largest Integer not greater than the number : "+Math.floor(i));
  }

Program 4
/*Write a program to generate 5 Random nos. between 1 to 100, and it
  should not follow with decimal point.
*/
class RandomDemo{
      public static void main(String args[]){
          for(int i=1;i<=5;i++){
              System.out.println((int)(Math.random()*100));
          }
    }
}

Program 5
/* Write a program to display a greet message according to
   Marks obtained by student.
*/
class SwitchDemo{
      public static void main(String args[]){
          int marks = Integer.parseInt(args[0]);                //take marks as command line argument.
         switch(marks/10){
            case 10:
            case 9:
            case 8:
                     System.out.println("Excellent");
                     break;
            case 7:
                     System.out.println("Very Good");
                     break;
            case 6:
                     System.out.println("Good");
                     break;
            case 5:
                     System.out.println("Work Hard");
                     break;
            case 4:
                     System.out.println("Poor");
                     break;
            case 3:
            case 2:
            case 1:
            case 0:
                     System.out.println("Very Poor");
                     break;
            default:
                     System.out.println("Invalid value Entered");
      }
 }
}

Program 6
/*Write a program to find SUM AND PRODUCT of a given Digit. */
class Sum_Product_ofDigit{
      public static void main(String args[]){
            int num = Integer.parseInt(args[0]);         //taking value as command line argument.
            int temp = num,result=0;
            //Logic for sum of digit
            while(temp>0){
               result = result + temp;
               temp--;
            }
            System.out.println("Sum of Digit for "+num+" is : "+result);
            //Logic for product of digit
            temp = num;
            result = 1;
            while(temp > 0){
                 result = result * temp;
                 temp--;
            }
            System.out.println("Product of Digit for "+num+" is : "+result);
   }
}

Program 7
/*Write a program to Find Factorial of Given no. */
class Factorial{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);                 //take argument as command line
          int result = 1;
          while(num>0){
                result = result * num;
                num--;
          }
          System.out.println("Factorial of Given no. is : "+result);
   }
}

Program 8
/*Write a program to Reverse a given no. */
class Reverse{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);               //take argument as command line
          int remainder, result=0;
          while(num>0){
              remainder = num%10;
              result = result * 10 + remainder;
              num = num/10;
         }
         System.out.println("Reverse number is : "+result);
    }
}

Program 9
/*Write a program to find Fibonacci series of a given no.
  Example :
        Input - 8
        Output - 1 1 2 3 5 8 13 21
*/
class Fibonacci{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);                        //taking no. as command line argument.
          System.out.println("*****Fibonacci Series*****");
          int f1, f2=0, f3=1;
          for(int i=1;i<=num;i++){
             System.out.print(" "+f3+" ");
             f1 = f2;
             f2 = f3;
             f3 = f1 + f2;
          }
   }
}

Program 10
/* Write a program to find sum of all integers greater than 100 and
   less than 200 that are divisible by 7 */
class SumOfDigit{
      public static void main(String args[]){
      int result=0;
      for(int i=100;i<=200;i++){
           if(i%7==0)
              result+=i;
      }
      System.out.println("Output of Program is : "+result);
   }
}