Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- *A Simple Java program to explain basic MultiThreading.
- *@author Steve Robinson
- *@see footyntech.wordpress.com
- */
- class ThreadTest implements Runnable
- {
- public int buffer=0;
- public void run()
- {
- try
- {
- for(int i=0;i<50;i++)
- {
- buffer+=i;
- Thread.sleep(400);
- }
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- System.out.println("Child Thread Ended");
- }
- }
- class MultiThreading
- {
- public static void main(String arg[])
- {
- ThreadTest temp=new ThreadTest();
- Runnable r=temp;
- Thread thread=new Thread(r);
- thread.start();
- String in;
- Scanner input=new Scanner(System.in);
- System.out.println("Press \"p\" to print current value and \"q\" to quit.. ");
- while(!((in=input.nextLine()).equals("q")))
- {
- if(in.equals("p"))
- {
- System.out.println("Current Value: " + temp.buffer);
- }
- }
- System.out.println("Stopping Child Thread....\nExiting....");
- //Deprecated method. But completely safe for such small programs. Please ignore warnings...
- thread.stop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement