Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How can I run two methods at the same time?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question How can I run two methods at the same time?

    Below is the main class of a project ive been working on, the goal is to start a countdown specified by the user. When the countdown reaches zero the base drops in the song that is being played. (Its not done yet) The main problem that arises is the fact that my song plays, and AFTER that, the timer starts.

    Output:
    Please input countdown in HH:mm:ss format.
    00:00:41
    Start?
    Yes
    The name of of the song is: Skrillex & Damian "Jr Gong" Marley - "Make It Bun Dem"
    The time of base drop is: 00:00:41 //Song starts here
    //Song is done
    //Then timer starts
    00:00:41
    00:00:40
    00:00:39

    Thank you,
    littlepresman

    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class AI 
    {
     
    	public static void main(String[] args)
    	{
     
    		System.out.println("Please input countdown in HH:mm:ss format.");
    	    Scanner sc = new Scanner(System.in);
    	    String input = sc.nextLine();
     
     
    	    songsBD playedSong = new songsBD(null, null, 0);
     
    	    String yes = "Yes";
     
    	    System.out.println("Start?");
    	    Scanner sc2 = new Scanner(System.in);
    	    String input2 = sc2.nextLine();
     
     
    	    MP3 SkrillexDamian = new MP3("/home/luke/Desktop/CompSci/Music/MakeitBurnDem.mp3");  
     
    	    if (input2.contains(yes))
    	    {
    	    	playedSong = choose(convertTime(input));
    	    	System.out.println(playedSong);
    	    	//Song starts here
     
    	    	String sd = "Skrillex";
     
    	    	if(playedSong.get_name_of_song().contains(sd)) ///////////////////////////////////Plays the song  **
    	    	{ 
    		    	SkrillexDamian.play();
    		    }
     
    	    	countDown(convertTime(input)); ///////////////////////////////////////////////////////////Starts the timer  **
     
    	    }
    	}
     
     
    	public static double convertTime(String input)
    	{
    		String numbers[] = input.split(":");
    		String hr = numbers[0];
    		String min = numbers[1];
    		String sec = numbers[2];
     
    		int intHr = Integer.parseInt(hr);
    		int intMin = Integer.parseInt(min);
    		int intSec = Integer.parseInt(sec);
    		int totalMin = intHr * 60 + intMin;
    		int totalSec = totalMin * 60 + intSec;
     
    		return totalSec;
    	}
     
    	public static void countDown(double sec)
        {
     
            for (double z = sec; z > 0; z--)
            {
                try 
                {
    				Thread.sleep(1000);
    			} 
                catch (InterruptedException e) 
    			{
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
                String s = convertSec(z);
                System.out.println(s);
            }
            //Base drop starts here
            System.out.println("Base drop.");
     
        }
     
        public static String convertSec(double z)
        {
        	int hours = (int) z / 3600;
        	int minutes = (int) (z / 60) %60;
        	int seconds = (int) z  % 60;
        	String res = hours + ":" + minutes + ":" + seconds;
    		return res;
        }
     
        public static songsBD choose(double d)
        {
        	ArrayList<songsBD> list = new ArrayList<songsBD>();
        	list.add(new songsBD("Skrillex & Damian \"Jr Gong\" Marley - \"Make It Bun Dem\"", "00:00:41", 41) );
     
        	for (int i = 0; i < list.size(); i++) 
        	{
        		  if (list.get(i).get_sec() == d) 
        		  {
        			  return list.get(i);
        		  }
        	}
     
        	songsBD noSong = new songsBD("There is no song for this amount of time.", "00:00:00", 0);
    		return noSong;
     
        }
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How can I run two methods at the same time?

    Your code is always executed in sequence.
    A line of code is only executed after the lines before have been executed. If you want things to run in parallel you have to learn how to use Threads.
    I suggest you read a couple of tutorials on the matter.

  3. The Following User Says Thank You to Cornix For This Useful Post:

    littlepresman (June 22nd, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How can I run two methods at the same time?

    1. Get the user's input for the countdown time.
    2. Start playing the song in one thread.
    3. Start the countdown timer in another thread.
    4. When the countdown timer expires, change the bass level.

    The threads in steps 2 and 3 start essentially at the same time, not sequentially.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    littlepresman (June 22nd, 2014)

Similar Threads

  1. Replies: 1
    Last Post: April 6th, 2014, 02:49 AM
  2. Run time error
    By gautammuktsar@gmail.com in forum Java Theory & Questions
    Replies: 2
    Last Post: February 10th, 2014, 04:31 PM
  3. Run time error
    By kmaka in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2013, 07:19 PM
  4. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  5. Help with run time error
    By white97 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 11th, 2011, 12:35 PM

Tags for this Thread