<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ritwin59</title>
    <description>The latest articles on DEV Community by ritwin59 (@ritwin59).</description>
    <link>https://dev.to/ritwin59</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F629450%2F3114147d-7924-4a68-984d-28a0cd6ed9aa.png</url>
      <title>DEV Community: ritwin59</title>
      <link>https://dev.to/ritwin59</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ritwin59"/>
    <language>en</language>
    <item>
      <title>#Multithreading in Java</title>
      <dc:creator>ritwin59</dc:creator>
      <pubDate>Thu, 25 Nov 2021 13:25:14 +0000</pubDate>
      <link>https://dev.to/ritwin59/multithreading-in-java-5265</link>
      <guid>https://dev.to/ritwin59/multithreading-in-java-5265</guid>
      <description>&lt;h2&gt;
  
  
  Multithreading
&lt;/h2&gt;

&lt;p&gt;Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.&lt;/p&gt;

&lt;h1&gt;
  
  
  Threads can be created by using two mechanisms :
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.Extending the Thread class &lt;br&gt;
2.Implementing the Runnable Interface&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Extending the Thread class&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;Thread creation by extending the Thread class&lt;br&gt;
We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.&lt;/p&gt;

&lt;h1&gt;
  
  
  Example:
&lt;/h1&gt;

&lt;p&gt;class MultithreadingDemo extends Thread {&lt;br&gt;
    public void run()&lt;br&gt;
    {&lt;br&gt;
        try {&lt;br&gt;
            // Displaying the thread that is running&lt;br&gt;
            System.out.println(&lt;br&gt;
                "Thread " + Thread.currentThread().getId()&lt;br&gt;
                + " is running");&lt;br&gt;
        }&lt;br&gt;
        catch (Exception e) {&lt;br&gt;
            // Throwing an exception&lt;br&gt;
            System.out.println("Exception is caught");&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
} &lt;br&gt;
// Main Class&lt;br&gt;
public class Multithread {&lt;br&gt;
    public static void main(String[] args)&lt;br&gt;
    {&lt;br&gt;
        int n = 8; // Number of threads&lt;br&gt;
        for (int i = 0; i &amp;lt; n; i++) {&lt;br&gt;
            MultithreadingDemo object&lt;br&gt;
                = new MultithreadingDemo();&lt;br&gt;
            object.start();&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Thread 15 is running&lt;br&gt;
Thread 14 is running&lt;br&gt;
Thread 16 is running&lt;br&gt;
Thread 12 is running&lt;br&gt;
Thread 11 is running&lt;br&gt;
Thread 13 is running&lt;br&gt;
Thread 18 is running&lt;br&gt;
Thread 17 is running&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementing the Runnable Interface
&lt;/h1&gt;

&lt;p&gt;Thread creation by implementing the Runnable Interface&lt;br&gt;
We create a new class which implements java.lang.Runnable interface and override run() method. Then we instantiate a Thread object and call start() method on this object. &lt;/p&gt;

&lt;p&gt;// Java code for thread creation by implementing&lt;br&gt;
// the Runnable Interface&lt;br&gt;
class MultithreadingDemo implements Runnable {&lt;br&gt;
    public void run()&lt;br&gt;
    {&lt;br&gt;
        try {&lt;br&gt;
            // Displaying the thread that is running&lt;br&gt;
            System.out.println(&lt;br&gt;
                "Thread " + Thread.currentThread().getId()&lt;br&gt;
                + " is running");&lt;br&gt;
        }&lt;br&gt;
        catch (Exception e) {&lt;br&gt;
            // Throwing an exception&lt;br&gt;
            System.out.println("Exception is caught");&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Main Class&lt;br&gt;
class Multithread {&lt;br&gt;
    public static void main(String[] args)&lt;br&gt;
    {&lt;br&gt;
        int n = 8; // Number of threads&lt;br&gt;
        for (int i = 0; i &amp;lt; n; i++) {&lt;br&gt;
            Thread object&lt;br&gt;
                = new Thread(new MultithreadingDemo());&lt;br&gt;
            object.start();&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
Output&lt;/p&gt;

&lt;p&gt;Thread 13 is running&lt;br&gt;
Thread 11 is running&lt;br&gt;
Thread 12 is running&lt;br&gt;
Thread 15 is running&lt;br&gt;
Thread 14 is running&lt;br&gt;
Thread 18 is running&lt;br&gt;
Thread 17 is running&lt;br&gt;
Thread 16 is running&lt;/p&gt;

&lt;h1&gt;
  
  
  Thread Class vs Runnable Interface
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes.&lt;/li&gt;
&lt;li&gt;We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.&lt;/li&gt;
&lt;li&gt;Using runnable will give you an object that can be shared amongst multiple threads. &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>multithreading</category>
    </item>
    <item>
      <title>User Defined Function In C</title>
      <dc:creator>ritwin59</dc:creator>
      <pubDate>Tue, 11 May 2021 12:46:10 +0000</pubDate>
      <link>https://dev.to/ritwin59/user-defined-function-in-c-570e</link>
      <guid>https://dev.to/ritwin59/user-defined-function-in-c-570e</guid>
      <description>&lt;p&gt;C programming language allows coders to define functions to perform special tasks. As functions are defined by users, they are called user-defined functions.&lt;/p&gt;

&lt;p&gt;user-defined functions have contained the block of statements which are written by the user to perform a task&lt;/p&gt;

&lt;p&gt;function has three related elements, in order to establish the function&lt;/p&gt;

&lt;p&gt;Function declaration&lt;br&gt;
function call&lt;br&gt;
Function definition&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;EXAMPLE:&lt;br&gt;
include &lt;br&gt;
include &lt;br&gt;
int sumNum(int x,int y); //function declaration&lt;br&gt;
int main()&lt;br&gt;
{&lt;br&gt;
    int i,j,sum;&lt;br&gt;
    printf("Please enter 2 numbers for find sum\n");&lt;br&gt;
    scanf("%d %d",&amp;amp;i,&amp;amp;j);&lt;br&gt;
    sum=sumNum(i,j); //function call&lt;br&gt;
    printf("The result of sum is :%d",sum);&lt;br&gt;
    getch();&lt;br&gt;
    return 0;&lt;br&gt;
}&lt;br&gt;
int sumNum(int x, int y)//function definition&lt;br&gt;
{&lt;br&gt;
    int result;&lt;br&gt;
result=x+y;&lt;br&gt;
return result; //return statements&lt;br&gt;
}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Declaration of the user-defined function.
&lt;/h1&gt;

&lt;p&gt;A function declaration is a frame (prototype)of function that contains the function’s name, list of parameter and return type and ends with the semicolon. but it doesn’t,t contain the function body&lt;/p&gt;

&lt;p&gt;Syntax&lt;/p&gt;

&lt;p&gt;return_Type function_Name(parameter1,parameter2,......);&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;int  sum_Num(int x, int y,.........);&lt;/p&gt;

&lt;h1&gt;
  
  
  Calling a function
&lt;/h1&gt;

&lt;p&gt;Syntax&lt;/p&gt;

&lt;p&gt;function_Name(Argument list);&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;sum_Num(argument_1, argument_2,.......);&lt;/p&gt;

&lt;h1&gt;
  
  
  Function Definition
&lt;/h1&gt;

&lt;p&gt;The function definition is an expansion of function declaration. It contains codes in the body part of the function for execution program by the compiler – it contains the block of code for the special task&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;return_Type function_name(parameter_1, parameter_2,....){&lt;br&gt;
//statements&lt;br&gt;
//body of the function&lt;br&gt;
}&lt;/p&gt;

&lt;h1&gt;
  
  
  Return statements:
&lt;/h1&gt;

&lt;p&gt;Return statements terminate the execution of the function and return value to calling the function. Also, return statements control of the program control and moved to the calling function.&lt;/p&gt;

&lt;p&gt;Syntax&lt;/p&gt;

&lt;p&gt;return (expression);&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;return (result);&lt;br&gt;
return (x+y);&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;include &lt;br&gt;
include &lt;br&gt;
int division(int x,int y); //function declaration or prototype&lt;br&gt;
int main()&lt;br&gt;
{&lt;br&gt;
    int i,j,div;&lt;br&gt;
    printf("Please enter 2 numbers for division\n");&lt;br&gt;
    scanf("%d%d",&amp;amp;i,&amp;amp;j);&lt;br&gt;
    div=division(i,j); //function call&lt;br&gt;
    printf("The result of division is :%d",div);&lt;br&gt;
    getch();&lt;br&gt;
    return 0;&lt;br&gt;
}&lt;br&gt;
int division(int i, int j)&lt;br&gt;
{&lt;br&gt;
    int result;&lt;br&gt;
result=i/j;&lt;br&gt;
return result; //return statements&lt;br&gt;
}&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
