www.fatihkabakci.com

Personal Website and Computer Science TUR EN

JAVA RUNTIME ve PROCESS

Last update: 10/3/2014 6:29:00 PM

Yazan:Fatih KABAKCI

Java programlama dili platform bağımsızlığını ve taşınabilirliği sağlayan java çalışma zamanını bir yere kadar kontrol etmeye ve yönetmeye izin vermektedir.İşletim sistemi içerisinde bazı proseslerin çalışması,askıya alınması,kapatılması,aktif duruma getirilmesi gibi bir takım işlemleri Java Sanal Makinesine erişerek gerçekleştirebilirsiniz.Bunun için Java kullanıcılarına Runtime,abstract sınıf olan Process ve ProcessBuilder gibi daha bir çok sınıfı sunmaktadır.

Bu yazıda örnek programlar üzerinden yukarıdaki bahsi geçen sınıflar açıklanacaktır.

İlk örnekte ortam değişkenleri ile birlikte kullanılmış windows konsol ekranına çıktı aktaran echo komutu kullanılmıştır.Tabi ki bu komut Runtime exec() metodu ile gerçeklenmiştir.



Runtime sınıfı soyut bir sınıf olmasada private bir constructor'a sahiptir.Bu nedenle referans olarak kullanılmaktadır.Yukarıdaki örnekteki konsol çıktısı Person:Fatih KABAKCI olarak görülmektedir.

İkinci örnekte ise bu sefer exec() metodunun ortam değişkenleri olmadan kullanıldığı başka bir versiyonu gösterilmiştir.Bu versiyonda metot sadece komut argümanı almaktadır.





waitFor() metodu çalışan bir alt prosesin bitmesini bekler.Yukarıdaki örnekte C: dizini içerisinden çalıştırılan Hello.txt adlı dosya kullanıcı tarafından kapanana kadar program beklemektedir.



Aşağıdaki örnekte ise yine benzer bir şekilde windows echo time komutu ile konsol ekranında sistem saati gösterilmiştir.Bu işlem yine exec() metodu ile sağlanmıştır.



Runtime sınıfının çok kullanılan diğer metotlarını içeren bir örnek programda aşağıda gösterilmiştir.




addShutdownHook(Thread hook) ve removeShutdownHookThread hook) metotları Java Sanal Makinesi sonlanırken çalışacak/iptal edilecek alt threadleri belirtir.

Java çalışma zamanı program sırasında vadesi dolan,kullanılmayan nesnelerin finalize() metotlarını çağırarak onları yok eder.Yinede bazı durumlarda bu kontrolü kendi başınıza yapmak isteyebilirsiniz.Bunun için gc() garbage collaction çağırılır.Yukarıdaki programda gc metodu çağrıldıktan sonra kullanılabilir memory'de bir artış olduğu görülmektedir.Programın örnek bir çıktısı ise aşağıda verilmiştir.



Yukarıdaki çıktı sizin bilgisayarınızda elbette farklı olacaktır.

Bir processi sonlandırmak için destroy() metodu kullanılabilirken,o prosese ait çıkış kodu exitValue() metodu ile alınır.Bir processin hata ve çıktı akışını okuyabilir ve girdi akışına da çıktı gönderebilirsiniz.Bu işlemler aşağıdaki metotlar ile sağlanmaktadır.
  • InputStream getErrorStream()
  • InputStream getInputStream()
  • OutputStream getOutputStream()
  • int exitValue()
  • void destroy()



There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.