{"id":3754,"date":"2017-02-25T13:29:36","date_gmt":"2017-02-25T11:29:36","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/?p=3754"},"modified":"2017-02-25T13:29:36","modified_gmt":"2017-02-25T11:29:36","slug":"processhandle-und-prozess-ids","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2017\/02\/processhandle-und-prozess-ids\/","title":{"rendered":"ProcessHandle und Prozess-IDs"},"content":{"rendered":"<p>Ein ProcessHandle ist ein neuer Typ in Java 9, der native Prozesse identifiziert. Wir bekommen Exemplare entweder \u00fcber<\/p>\n<ul>\n<li>die Process-Objektmethode toHandle(),<\/li>\n<li>die statische Methodecurrent(),<\/li>\n<li>allProcesses(), die alle Prozesse \u00fcber einen Stream&lt;ProcessHandle&gt; liefert,<\/li>\n<li>die statische ProcessHandle-Methode of(long pid), die uns das ProcessHandle in ein Optional verpackt,<\/li>\n<li>mit einem vorhandenen ProcessHandle k\u00f6nnen wir weiterhin mit children() und descendants() einen Stream&lt;ProcessHandle&gt; erfragen und mit parent() auf die Eltern zugreifen.<\/li>\n<\/ul>\n<p>Jeder Prozess verf\u00fcgt \u00fcber eine identifizierende long-Ganzahl, die getPid() auf dem ProcessHandle liefert. Weitere Details zu den Startparametern offenbare ProcessHandle.Info-Ojekte.<\/p>\n<p>Beispiel:\u00a0Gib alle vorhanden Informationen \u00fcber alle Prozesse aus:<\/p>\n<pre>Consumer&lt;ProcessHandle&gt; log = handle -&gt;\n\n\u00a0 System.out.printf( \"PID=%s, Root?=%s, Info=%s%n\",\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 handle.getPid(), !handle.parent().isPresent(), handle.info() );\n\nProcessHandle.allProcesses().forEach( log );<\/pre>\n<p>Die Ausgabe kann so aussehen:<\/p>\n<pre>PID=0, Root?=true, Info=[]\n\nPID=4, Root?=true, Info=[]\n\n...\n\nPID=4368, Root?=true, Info=[]\n\nPID=4568, Root?=true, Info=[user: Optional[Yoga\\Christian], cmd: C:\\Windows\\System32\\sihost.exe, startTime: Optional[2017-02-04T20:54:07.601Z], totalTime: Optional[PT3M39.703125S]]\n\nPID=4592, Root?=true, Info=[user: Optional[Yoga\\Christian], cmd: C:\\Windows\\System32\\svchost.exe, startTime: Optional[2017-02-04T20:54:07.621Z], totalTime: Optional[PT14.9375S]]\n\nPID=4628, Root?=true, Info=[]\n\n...<\/pre>\n<p>ProcessHandle implementiert vern\u00fcnftig equals(\u2026) und auch Comparable&lt;ProcessHandle&gt;; die Sortierung ist nach der Prozess-ID.<\/p>\n<p>Methoden-Delegation<\/p>\n<p>Einige Methoden aus Process delegieren an den assoziierten ProcessHandle. Die Methoden hei\u00dfen gleich.<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"298\">Process-Methoden<\/td>\n<td width=\"298\">Implementierung<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">long getPid()<\/td>\n<td width=\"298\">return toHandle().getPid();<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">ProcessHandle.Info info()<\/td>\n<td width=\"298\">return toHandle().info();<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">Stream&lt;ProcessHandle&gt; children()<\/td>\n<td width=\"298\">return toHandle().children();<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">Stream&lt;ProcessHandle&gt; descendants()<\/td>\n<td width=\"298\">return toHandle().descendants();<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Weiterhin gibt es onExit(), supportsNormalTermination(), isAlive(), destroy() und destroyForcibly() auf beiden Typen Process und ProcessHandle.<\/p>\n<p>&nbsp;<\/p>\n<p>\u00dcber Objekte vom Typ ProcessHandle.Info lassen sich weitere Details zum Prozess erfragen; die R\u00fcckgaben sind Optional, weil die Informationen vielleicht nicht vorliegen.<\/p>\n<ul>\n<li>static interface java.lang.<strong>Info<\/strong><\/li>\n<li>Optional&lt;String[]&gt; arguments()<br \/>\nProgrammargumente beim Start.<\/li>\n<li>Optional&lt;String&gt; command()<br \/>\nAusf\u00fchrbarer Pfadname vom Prozess.<\/li>\n<li>Optional&lt;String&gt; commandLine()<br \/>\nKonkatenation von command() und arguments() beste Repr\u00e4sentation des Programmaufrufs.<\/li>\n<li>Optional&lt;Instant&gt; startInstant()<br \/>\nStartzeit des Prozesses.<\/li>\n<li>Optional&lt;Duration&gt; totalCpuDuration()<br \/>\nBisher verbrauchte CPU-Zeit.<\/li>\n<li>Optional&lt;String&gt; user()<br \/>\nBenutzer dieses Prozesses.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ein ProcessHandle ist ein neuer Typ in Java 9, der native Prozesse identifiziert. Wir bekommen Exemplare entweder \u00fcber die Process-Objektmethode toHandle(), die statische Methodecurrent(), allProcesses(), die alle Prozesse \u00fcber einen Stream&lt;ProcessHandle&gt; liefert, die statische ProcessHandle-Methode of(long pid), die uns das ProcessHandle in ein Optional verpackt, mit einem vorhandenen ProcessHandle k\u00f6nnen wir weiterhin mit children() und [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[1,11,85],"tags":[],"class_list":["post-3754","post","type-post","status-publish","format-standard","hentry","category-allgemein","category-insel","category-java-9"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/3754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=3754"}],"version-history":[{"count":3,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/3754\/revisions"}],"predecessor-version":[{"id":3757,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/3754\/revisions\/3757"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=3754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=3754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=3754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}