{"id":932,"date":"2010-05-08T13:43:33","date_gmt":"2010-05-08T13:43:33","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=932"},"modified":"2010-05-08T13:43:33","modified_gmt":"2010-05-08T13:43:33","slug":"threading-run-another-part-of-the-program-in-a-separate-process","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/08\/threading-run-another-part-of-the-program-in-a-separate-process\/","title":{"rendered":"Threading &#8211; run another part of the program in a separate process"},"content":{"rendered":"<p><span id=\"zipfile\"><a href='http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/threading.zip'><\/a><\/span>Threading in Java is very similar to the threading in c#, just like the example that I did before (<a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/05\/07\/threading-running-different-parts-of-the-program-at-once\/\">threading in c sharp, running different-parts-of-the-program-at-once<\/a>.  What threading does is to allow for a part of your program to be run from within another process, this process runs normally independently of the main program (you can have links if you wanted to so some memory references).  <\/p>\n<p>The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, e.g. 10 milliseconds, and then leave the CPU execution stage whilst another process will enter there CPU execution stage (this is the part where the program is actually doing something).<\/p>\n<p>So lets say that you want to spawn off another process that will talk to a server to get information back e.g. a tab browsing on a modern web browser, whilst you look at another part of the main program e.g another tab, and communicate with that one e.g. type in your login details etc.<\/p>\n<p>So let start with a example of how to create a thread-able program.  Here my class basicThread extends the class Thread, which encapsulate the thread-able interface.<\/p>\n<pre lang=\"java\">\r\npublic class basicThread extends Thread {\r\n<\/pre>\n<p>this thread-able interface basically only wants to have the method &#8220;run&#8221; implemented&#8221;, since that is what is called when you start the new thread.<\/p>\n<pre lang=\"java\">\r\n\tpublic void run()\r\n\t{\r\n\t\tSystem.out.println(\"Before my sleep\");\r\n\t\tfor (int i =0; i < 10; i++)\r\n\t\t{....\r\n<\/pre>\n<p>and then in your main program you just need to create a basicThread class and call the start method (from the Thread class that basicThread class inherited from)<\/p>\n<pre lang=\"java\">\r\n\t\t\/\/ create the thread, a thread is independent of the main program\r\n\t\tbasicThread me = new basicThread();\r\n\t\t\/\/ then start it. this calls the run method\r\n\t\tme.start();\r\n<\/pre>\n<p>and that is it, it will spawn off another process that will run separately in time from the main process.<\/p>\n<p>Here is the full source code, I have also included a file zip that has the source code and also the class files attached, save this as callThread.java<\/p>\n<pre lang=\"java\">\r\npublic class callThread {\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/ create the thread, a thread is independent of the main program\r\n\t\tbasicThread me = new basicThread();\r\n\t\t\/\/ then start it. this calls the run method\r\n\t\tme.start();\r\n\r\n\t\t\/\/ so if the basic thread had that pause it in..\r\n\t\tSystem.out.println(\"main thread here!!\");\r\n\t\tfor (int j =0; j < 5; j++)\r\n\t\t{\t\t\r\n\t\t\tSystem.out.println(\"Mainthread about to sleep at number : \"+j);\r\n\t\t\t\/\/ need to pause the main thread\r\n\t\t\ttry{\r\n\t\t\t  Thread.currentThread().sleep(100);\/\/sleep for 1000 ms\r\n\t\t\t}\tcatch(Exception ie){\r\n\t\t\t}\r\n\t\t}\r\n\t\tSystem.out.println(\"End of the main thread\");\r\n\t\t\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>save this as basicThread.java<\/p>\n<pre lang=\"java\">\r\npublic class basicThread extends Thread {\r\n\tpublic void run()\r\n\t{\r\n\t\tSystem.out.println(\"Before my sleep\");\r\n\t\tfor (int i =0; i < 10; i++)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"basicThread sleeping at number : \"+i);\r\n\t\t\ttry {\r\n\t\t\t\tsleep(45);\r\n\t\t\t} catch (InterruptedException e)\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"could not sleep long enought there was a error!!\");\r\n\t\t\t}\r\n\t\t\t\r\n\t\t}\r\n\t\tSystem.out.println(\"after the sleep :)\");\r\n\t}\r\n}\r\n<\/pre>\n<p>here is the output, as you can see the mainthread and the basicthread outputs interrupt each other just like any other process does within a CPU.<\/p>\n<pre lang=\"bash\">\r\nmain thread here!!\r\nBefore my sleep\r\nMainthread about to sleep at number : 0\r\nbasicThread sleeping at number : 0\r\nbasicThread sleeping at number : 1\r\nbasicThread sleeping at number : 2\r\nMainthread about to sleep at number : 1\r\nbasicThread sleeping at number : 3\r\nbasicThread sleeping at number : 4\r\nMainthread about to sleep at number : 2\r\nbasicThread sleeping at number : 5\r\nbasicThread sleeping at number : 6\r\nMainthread about to sleep at number : 3\r\nbasicThread sleeping at number : 7\r\nbasicThread sleeping at number : 8\r\nMainthread about to sleep at number : 4\r\nbasicThread sleeping at number : 9\r\nafter the sleep :)\r\nEnd of the main thread\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Threading in Java is very similar to the threading in c#, just like the example that I did before (threading in c sharp, running different-parts-of-the-program-at-once. What threading does is to allow for a part of your program to be run from within another process, this process runs normally independently of the main program (you can &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/08\/threading-run-another-part-of-the-program-in-a-separate-process\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Threading &#8211; run another part of the program in a separate process<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[203,204,201,205],"class_list":["post-932","post","type-post","status-publish","format-standard","hentry","category-java","tag-extends-thread","tag-sleep","tag-thread-start","tag-threading"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=932"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/932\/revisions"}],"predecessor-version":[{"id":935,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/932\/revisions\/935"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}