1package com.mkyong.io;
2
3import java.util.Scanner;
4
5public class JavaScanner {
6
7 public static void main(String[] args) {
8
9 Scanner scanner = new Scanner(System.in);
10
11 String input = "";
12 while (!"q".equalsIgnoreCase(input)) {
13
14 System.out.print("Enter something (q to quite): ");
15
16 input = scanner.nextLine();
17 System.out.println("input : " + input);
18 }
19
20 System.out.println("bye bye!");
21 }
22
23}
1Multithreading is a model of program execution
2that allows for multiple threads to be created
3within a process, executing independently but
4concurrently sharing process resources.
5Depending on the hardware, threads can run
6fully parallel if they are distributed to their own CPU core.
1import threading
2
3def func1():
4 # your function
5def func2():
6 # your function
7if __name__ == '__main__':
8 threading.Thread(target=func1).start()
9 threading.Thread(target=func2).start()
1class Count implements Runnable
2{
3 Thread mythread ;
4 Count()
5 {
6 mythread = new Thread(this, "my runnable thread");
7 System.out.println("my thread created" + mythread);
8 mythread.start();
9 }
10 public void run()
11 {
12 try
13 {
14 for (int i=0 ;i<10;i++)
15 {
16 System.out.println("Printing the count " + i);
17 Thread.sleep(1000);
18 }
19 }
20 catch(InterruptedException e)
21 {
22 System.out.println("my thread interrupted");
23 }
24 System.out.println("mythread run is over" );
25 }
26}
27class RunnableExample
28{
29 public static void main(String args[])
30 {
31 Count cnt = new Count();
32 try
33 {
34 while(cnt.mythread.isAlive())
35 {
36 System.out.println("Main thread will be alive till the child thread is live");
37 Thread.sleep(1500);
38 }
39 }
40 catch(InterruptedException e)
41 {
42 System.out.println("Main thread interrupted");
43 }
44 System.out.println("Main thread run is over" );
45 }
46}