Anda di halaman 1dari 4

Using Thread in Java Based Application

Multithreading - An Efficient Technique for


Enhancing Application Performance
Harris V. Sibuea

Samuel Christian Silalahi

Computer Science
Del Institute of Technology
Laguboti, 22381 Indonesia
If413007@students.del.ac.id

Computer Science
Del Institute of Technology
Laguboti, 22381 Indonesia
If413030@students.del.ac.id

Eplianto Butar-butar

Marthin Pasaribu

Computer Science
Del Institute of Technology
Laguboti, 22381 Indonesia
If413034@students.del.ac.id

Computer Science
Del Institute of Technology
Laguboti, 22381 Indonesia
If413073@students.del.ac.id

Jen Presly Samosir


Computer Science
Del Institute of Technology
Laguboti, 22381 Indonesia
If413076@students.del.ac.id
Abstract The main aim of this paper is to study, to implement
thread in Java based application and to know the advantages of using
thread in Java based application. In carrying out this research, there
are several things that need to be prepared like collecting data, which
collects data about the thread, and the thread usage in software
development. After that, the data collected through the information in
reference books, and internet sites. To make a comparison of
application that uses that do not use the thread and thread, then do an
analysis of the survey results, especially in the thread function in the
software and the difference between the software that uses thread and
that does not use threads. Based on the finding, we know that using
thread in java based application can control the program flow using
single control. Using thread in java based application makes the
application faster.
Keywords Thread, Java Thread, Java Based Application

I. INTRODUCTION
Over the last few years, Java has emerged as an interesting
language for the network programming community. This has
largely occurred as a direct consequence of the design of the
Java language. This design includes, among others, important
aspects such as portability and architecture neutrality of Java
code, and its multithreading facilities. The latter is achieved
through built-in support for threads in the language definition.
The Java library provides the Thread class definition, and Java
runtimes provide support for thread and monitor primitives.
These characteristics, besides others like its familiarity (due to
its resemblance with C/C++), its robustness, security and

distributed nature also make it a potentially interesting


language for parallel scientific computing.
A thread is a point of execution within a process. A multithreaded process has multiple points of concurrent execution
within the process [2]. Multi-threading is useful in any
situation where a single thread has to wait for a resource and
we can run another thread in the meantime. This includes a
thread waiting for an I/O request or database access while
another thread continues with CPU work. Multi-threading is
also useful if the individual threads can be farmed out to
different CPUs (or cores) as they then run truly concurrently
[2]. Multithreading involves new way of programming the
parallel execution of tasks and how they interact with each
other vs. the traditional line-by-line execution of code. As
programmers add more threads to their applications, the
complexity of managing those threads jumps exponentially.
The situation is analogous to a single developer working on a
project vs. a team of developers working on the same project.
The team can accomplish more in less time, but the members
must communicate among themselves to avoid duplication of
effort and prevent any accidental collisions in their work.
Regardless of the development environment, multithreading
presents many important advantages to developers of Banking
and other applications. Developers creating multithreaded
applications can use their computer-based systems to the
fullest potential as companies continue to standardize on
multithreaded operating systems or acquire increasingly
affordable multiprocessor computers to boost performance [4].

In creating a multithreaded algorithm, there are three basic


considerations [2]. The first step is to identify the parallelism.
This may meant simply decomposing the problem domain of a
conventional algorithm into several sections. The second step
is to control the access to shared data items. The third step is
to optimize the algorithm. The main aim of this paper is to
study, to implement thread in Java based application and to
know the advantages of using thread in Java based application.
II.

LITERATURE REVIEW

A. What is Thread?
Conceptually, the notion of a thread is not difficult to
grasp: it is an independent path of execution through program
code [2]. When multiple threads execute, one thread's path
through the same code usually differs from the others. For
example, suppose one thread executes the byte code
equivalent of an if-else statement's ifpart, while another thread
executes the byte code equivalent of the else part. How does
the JVM keep track of each thread's execution? The JVM
gives each thread its own method-call stack [4]. In addition to
tracking the current byte code instruction, the method-call
stack tracks local variables, parameters the JVM passes to a
method, and the method's return value. When multiple threads
execute byte-code instruction sequences in the same program,
that action is known as multithreading [2]. Multithreading
benefits a program in various ways [2]:

of squares, while the new thread executes the run() method to


print the right-angle triangle [3].
C. The Thread Class
To grow proficient at writing multithreaded code, you must
first understand the various methods that make up
the Thread class. This section explores many of those
methods. Specifically, you learn about methods for starting
threads, naming threads, putting threads to sleep, determining
whether a thread is alive, joining one thread to another thread,
and enumerating all active threads in the current thread's
thread group and subgroups [1].
D. How threads work?
To understand certain thread programming issues in more
detail, it's helpful to take a more detailed look at what threads
actually are and how they work.

Multithreaded GUI (graphical user interface)-based


programs remain responsive to users while
performing other tasks, such as repaginating or
printing a document.
Threaded programs typically finish faster than their
nonthreaded counterparts. This is especially true of
threads running on a multiprocessor machine, where
each thread has its own processor.

Java
accomplishes
multithreading
through
its java.lang.Thread class. Each Thread object describes a
single thread of execution. That execution occurs in Thread's
run() method. Because the default run() method does nothing,
you must subclass Thread and override run() to accomplish
useful work [3].
B. Thread Scheduling and the JVM
Most (if not all) JVM implementations use the underlying
platform's threading capabilities. Because those capabilities
are platform-specific, the order of your multithreaded
programs' output might differ from the order of someone else's
output. That difference results from scheduling.
When you type java Thread Demo to run the application,
the JVM creates a starting thread of execution, which executes
the main() method. By executing mt.start ();, the starting
thread tells the JVM to create a second thread of execution
that executes the byte code instructions comprising the My
Thread object's run() method [3]. When the start() method
returns, the starting thread executes its for loop to print a table

Figure 1: Typical relationsip between threads and processes.


E. The thread scheduler
There are generally more threads than CPUs. Part of a
multithreaded system is therefore a thread scheduler,
responsible for sharing out the available CPUs in some way
among the competing threads [1]. Note that in practically all
modern operating systems, the thread scheduler is part of the
OS itself. So the OS actually "sees" our different threads and
is responsible for the task of switching between them. The
rationale for handling threading "natively" in the OS is that the
OS is likely to have the information to make threading
efficient (such as knowing which threads are waiting for I/O
and for how long), whereas a software library may not have
this information available [3].

III. METODS
A. Technique of Collecting Data
There are several things that need to be prepared before
the process of data collection, which collects data about the
thread, and the thread usage in software development. After
that, the data collected through the information in reference
books, and as well as internet sites.
B. Technique of Analyzing Data
Survey software that uses thread theoretically produce
information about any software that uses the thread and the
thread functions in software. To make a comparison of
software that uses that do not use the thread and thread, then
do an analysis of the survey results, especially in the thread
function in the software. The results of this analysis are very
influential on conclusion of this study, and need to pay
attention to the difference between the software that uses
thread and that does not use threads.
IV.

FINDING AND DISCUSSION

The result of the research is displayed in this session.


A. Not Using Thread
The java application that not using thread cannot control
the program flow and cannot do some process in one time.
Actually, Java have one thread which automatic created, it is
called main thread.
public class threadTester {
public static void main(String[] args){
int[] arr1 = {1,2,3,4,5}; // array 1
int[] arr2 = {6,7,8,9,10}; // array 2
int[] arr3 = {11,12,13,14,15}; // array3
for (int i=0; i < arr1.length ; i++){
simpleThread st = new simpleThread("Thread "+i);
st.setNum(arr1[i], arr2[i], arr3[i]);
st.start();
}
}
}
B. Using Thread
The java application that using thread is more efficient.
The application can do some process in one time, but do not
make deadlock in the process. Thread also can control the
program flow. Built-in support for threads provides Java
programmers with a powerful tool to improve interactive
performance of graphical applications. If your application
needs to run animations and play music while scrolling the
page and downloading a text file from a server, multithreading
is the way to obtain fast, lightweight concurrency within a
single process space. Threads are sometimes also called
lightweight processes or execution contexts.

A thread can be created in java by extending Thread class,


where you must override run() method [3]. Call start() method
to start executing the thread object. This is example of the
application with the SimpleThread class, a subclass of the
Thread class that is provided by the java.lang package.
class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
The first method in the SimpleThread class is a constructor
that takes a String as its only argument. This constructor is
implemented by calling a superclass constructor and is only
interesting to us because it sets the Thread's name which is
used later in the program.
The next method in the SimpleThread class is the run()
method. The run() method is the heart of any Thread--it's
where the action of the Thread takes place. The run() method
of the SimpleThread class contains a for loop that iterates ten
times. In each iteration the method displays the iteration
number and the name of the Thread then sleeps for a random
interval between 0 and 1 second. After the loop has finished,
the run() method prints "DONE!" along with the name of the
thread. That's it for the SimpleThread class.
V. CONCLUSION AND RECOMENDATION
Based on the finding, we know that using thread in java based
application can control the program flow using single control
and do not make deadlock. Using thread in java based
application is recommended if we want to make faster
application.

REFERENCES
[1]

[2]
[3]
[4]
[5]

Balakrishnan, S. and S.K.Nandy, 2000. Performance evaluation of


multithreaded architectures for media processing applications. In: Proc.
IEEE. Int. Symp. Circ. Syst. ISCAS, 1: 531-534.
Allan McNaughton Multi-Threading in a Java* Environment 2007.
Soumya Guptha Comparative Study of Threading Models Software and
Solutions Group, Intel Corporation2007.
Guitart, F.J., 2005. Performance Improvement of Multithreaded Java
Applications Execution on Multiprocessor Systems Performance.
Abbott, Martin Lee and Jennifer McKinney. (2013). Understanding and
Applying Research Design. New Jersey: John Wiley and Sons Inc.

[6]

(2014, November 27) The Place Cutting-Edge Qualitative Research.


Retrieved from http://www.qrca.org/?page=whatisqualresearch.html

A. Technique of Collecting Data


There are several things that need to be prepared before the process of data collection, which collects data about the thread,
and the thread usage in software development. After that, the data collected through the information in reference books, and as
well as internet sites.
B. Technique of Analyzing Data
Survey software that uses thread theoretically produce information about any software that uses the thread and the thread
functions in software. To make a comparison of software that uses that do not use the thread and thread, then do an analysis of the
survey results, especially in the thread function in the software. The results of this analysis are very influential on conclusion of
this study, and need to pay attention to the difference between the software that uses thread and that does not use threads.
Metods
A.Teknik Pengumpulan Data
Ada beberapa hal yang perlu dipersiapkan sebelum proses pengumpulan data, yang
mengumpulkan data tentang benang, dan penggunaan benang dalam pengembangan perangkat
lunak. Setelah itu, data yang dikumpulkan melalui informasi dalam buku-buku referensi,
dan juga situs-situs internet.
B. Teknik Analisis data
Survei perangkat lunak yang menggunakan benang secara teoritis menghasilkan informasi
tentang perangkat lunak yang menggunakan benang dan fungsi benang dalam perangkat
lunak. Untuk membuat perbandingan perangkat lunak yang menggunakan yang tidak
menggunakan benang dan benang, kemudian melakukan analisis terhadap hasil survei,
terutama dalam fungsi benang dalam perangkat lunak. Hasil analisis ini sangat
berpengaruh pada kesimpulan penelitian ini, dan perlu memperhatikan perbedaan antara
software yang menggunakan benang dan yang tidak menggunakan benang.

Anda mungkin juga menyukai