title: Run jobs parallel in oracle
body: <p>I have 8 jobs which I have to run. At any instance, there must be 4 jobs running. One of the sample jobs is as follows:</p>
  
  <pre><code>CREATE OR REPLACE PROCEDURE job1
  AS
    vblSQL  VARCHAR(32767);
    vJobID  NUMBER:=1;
  
  BEGIN
    EXECUTE IMMEDIATE'insert into job_logs values('''||vJobID||''',sysdate,NULL)';
    vblSQL:= 'UPDATE jobs SET status=''R'' WHERE job_ID=1';
    EXECUTE IMMEDIATE(vblSQL);
    Dbms_Lock.sleep(45);
    vblSQL:= 'UPDATE jobs SET status=''Y'' WHERE job_ID=1';
    EXECUTE IMMEDIATE(vblSQL);
    vblSQL:= 'UPDATE job_logs SET endtime=SYSDATE WHERE jobID=1';
    EXECUTE IMMEDIATE(vblSQL);
  END;
  </code></pre>
  
  <p>Other 7 jobs are similar to this only varying in the <code>Job_ID</code>. I have written a code  which initializes four jobs to run at once. Here is the code:</p>
  
  <pre><code>CREATE OR REPLACE PROCEDURE sp_ProcessJobs
  AS
    vblSQL          VARCHAR2(32767);
    vblTotalJobs    VARCHAR2(1000);
    vblRemJobs      VARCHAR2(1000);
    vblJob1         VARCHAR2(1000);
    vblJob2         VARCHAR2(1000);
    vblJob3         VARCHAR2(1000);
    vblJob4         VARCHAR2(1000);
    vblJob          VARCHAR2(1000);
    lno             NUMBER;
  
  BEGIN
    SELECT Count(*) INTO vblTotalJobs FROM jobs WHERE status='N';
  
    FOR i IN 1..4 LOOP
        SELECT jobname INTO vblJob 
        FROM 
         (SELECT job_id,status,jobname,Rank() OVER (ORDER by job_id)rnk
          FROM
           (SELECT * from jobs WHERE status='N' ORDER BY job_id)
        )WHERE rnk=i; 
  
      IF    i=1 THEN vbljob1:=vbljob;
      ELSIF i=2 THEN vbljob2:=vbljob;
      ELSIF i=3 THEN vbljob3:=vbljob;
      ELSIF i=4 THEN vbljob4:=vbljob;
      END IF;
  
    END LOOP;
  
    Dbms_Job.submit(lno,'BEGIN '||vbljob1||'; END;');
    COMMIT;
    Dbms_Job.submit(lno,'BEGIN '||vbljob2||'; END;');
    COMMIT;
    Dbms_Job.submit(lno,'BEGIN '||vbljob3||'; END;');
    COMMIT;
    Dbms_Job.submit(lno,'BEGIN '||vbljob4||'; END;');
    COMMIT;
  
    Dbms_Output.put_line(vblTotalJobs);
  
  END;
  </code></pre>
  
  <p>This allows me to run four jobs parallel at once. Now what should I do to run another job(say Job no. 5) as soon as one job is completed and so on. Every time I try to do it, the program enters an infinite loop and I have to kill the session to stop it. </p>
  
  <p>What I am trying to achieve is similar to threading in JAVA or other programming languages. Any help would be highly appreciated.</p>
  
  <p>I am using Oracle.</p>