{"id":1153,"date":"2025-05-06T14:14:54","date_gmt":"2025-05-06T11:14:54","guid":{"rendered":"https:\/\/www.juustila.com\/antti\/?p=1153"},"modified":"2025-05-06T15:43:09","modified_gmt":"2025-05-06T12:43:09","slug":"long-break-status-update-demo","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2025\/05\/06\/long-break-status-update-demo\/","title":{"rendered":"Long break, status update &amp; demo"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">So, another long break in blogging. This, of course, is due to having too much work and spending the remaining energy somewhere else (like running).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So I thought I&#8217;d write a couple of posts on what I have been doing in between the previous post in January:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Evaluating the <strong>Data Structures and Algorithms<\/strong> course programming assignments. This has been faster this year, since my <a href=\"https:\/\/www.juustila.com\/antti\/2023\/10\/14\/multiple-git-repository-status-awareness-app\/\" data-type=\"post\" data-id=\"1035\">automated tool<\/a> that both unit tests all the submissions and now also checks the source code that it doesn&#8217;t use things students are <em>not<\/em> supposed to use, and <em>does use<\/em> things they <em>are<\/em> supposed to use. Later I&#8217;ll make a post about the latest and future improvements to this tool. I still use too much time on this, due to lacking teaching resources, so the only ways to improve the situation are to add more automation and forget about giving detailed feedback to students.<\/li>\n\n\n\n<li>Launching and supervising six <strong>BSc student projects<\/strong>. This started in January and will continue to end of April or May, depending on individual project schedules. Groups of 4-6 students show the skills and knowledge they (hopefully) acquired during their BSc studies and design and\/or implement a software for an external (or &#8220;external&#8221;) customer. Sometimes they continue development of already existing software. I have weekly status meetings with them and guide them through the project, and participate in official steering group meetings.<\/li>\n\n\n\n<li>Preparing and conducting our MSc program <strong>applicant interviews<\/strong>. This took most of the February, almost full day.<\/li>\n\n\n\n<li>In March, the <strong>Programming 4 course<\/strong> started. The course focuses on GUI design, usability and user experience. I am assisting my colleague in this course. Students design and implement a GUI app, often the first time in their lives. Up to this point, they have usually implemented only small command line apps with relatively small number of functions or classes. This week is the last week of the course.<\/li>\n\n\n\n<li>Supervising some BSc and MSc <strong>thesis<\/strong> students and<\/li>\n\n\n\n<li><strong>Preparing courses<\/strong> for teaching in Fall.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So, lots of work and thus no time for blogging, considering my priorities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Anyways, I thought that in addition to listing the jobs above, I could also show some demonstrations I prepared for the Programming 4 course. The one described below in this post is a very simple and small demonstration showing how to use a <strong>Publish\/Subscribe<\/strong> pattern to update GUI views when a data object (a model) in software changes state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The old school way to do this is the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Observer_pattern\">Observer<\/a> design pattern, which has it&#8217;s pros and cons &#8212; easy to use but has its negative consequences too. Classes related to that in Java have been deprecated though still usable. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of Observer, Java recommends using other techniques, like the Publish\/Subscribe pattern, using Java Flow <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/20\/docs\/api\/java.base\/java\/util\/concurrent\/Flow.Publisher.html\">Publisher<\/a> and <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/20\/docs\/api\/java.base\/java\/util\/concurrent\/Flow.Subscriber.html\">Subscriber<\/a> interfaces and classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, I implemented a simple demo app in Java and Swing, <a href=\"https:\/\/github.com\/anttijuu\/mvcclock\">WallClockApp<\/a> (GitHub link to source). The app uses the Publish\/Subsctibe pattern and looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"221\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2025\/05\/Nayttokuva-2025-05-06-kello-13.37.03-1024x221.png\" alt=\"\" class=\"wp-image-1154\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2025\/05\/Nayttokuva-2025-05-06-kello-13.37.03-1024x221.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2025\/05\/Nayttokuva-2025-05-06-kello-13.37.03-300x65.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2025\/05\/Nayttokuva-2025-05-06-kello-13.37.03-768x165.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2025\/05\/Nayttokuva-2025-05-06-kello-13.37.03-1200x259.png 1200w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2025\/05\/Nayttokuva-2025-05-06-kello-13.37.03.png 1346w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can add several timezones using the plus button, and see the time in these different timezones. The red circle and cross button in each timezone can be used to remove a timezone. Obviously, the times advance with each clock, as seconds tick forward.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The app follows the also common <a href=\"https:\/\/en.wikipedia.org\/wiki\/Model\u2013view\u2013controller\">Model-View-Controller<\/a> architecture in GUI apps. In the app the Model (class <code>ClockTickSource<\/code>) is an object that provides the time signal, once a second. The View objects (class <code>WallClock<\/code>) are the different time zone views (three visible in the screenshot above). The app object (class <code>WallClockApp<\/code>) acts as the Controller, creating the model, and adding clock Views for user selected timezones. The Views basically just show the time in their corresponding time zones.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Where does the Publish\/Subscribe then fits into this? Well, the Model needs a way to tell the views when a second has passed. So it has to know about the views that need the time tick signal. When the Model&#8217;s timer ticks, once a second, it then needs to notify the views so that they can update the time visible in their timezones.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This has been implemented here using the Java Publish\/Subscribe pattern. The model is the publisher, that publishes the current time in one second intervals. The current time is published as the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unix_time\">Unix time<\/a>, milliseconds since Jan 1st, 1970, in UTC time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Views, the Subscribers, subscribe to the Publisher as they are created:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public WallClock(final ClockTickSource clock, final TimeZone timeZone) {\n   super();\n   setLayout( new BorderLayout());\n   this.clock = clock;\n   this.timeZone = timeZone;\n   this.clock.subscribe(this);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On the last line above, you can see the View object subscribing to the clock ticks, calling the <code>clock.subscribe<\/code>. The Publisher (clock, the Model) then will notify the Subscriber about successful subscription and the View can then request for the clock ticks (<code>Long<\/code> values; the milliseconds from Jan 1st, 1970 UTC):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void onSubscribe(final Subscription subscription) {\n   this.subscription = subscription;\n   subscription.request(Long.MAX_VALUE);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Publisher then (code below) starts the clock ticking using a <code>Timer<\/code>, but only once, for the first subscriber (since only one timer is needed to run the show; see the <code>start()<\/code> method on details on how to launch the timer):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void subscribe(final Subscriber&lt;Long&gt; subscriber) {\n  if (publisher == null) {\n    publisher = new SubmissionPublisher&lt;Long&gt;(ForkJoinPool.commonPool(), 10);\n  }\n  publisher.subscribe(subscriber);\n  if (publisher.getNumberOfSubscribers() == 1) {\n    start();\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A concrete Java class <code><a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/21\/docs\/api\/java.base\/java\/util\/concurrent\/SubmissionPublisher.html\">SubmissionPublisher<\/a><\/code> does the actual work here in the Publisher side.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then the subscribers (views) handle the published data (Unix timestamps) as they get the notification from the Publisher when <code>onNext<\/code> is called by the Java Flow framework. The views will then convert the UTC time in milliseconds in the parameter <code>item<\/code> to their respective timezones and show that to the user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void onNext(final Long item) {\n   final long currentTime = item;\n   Date time = new Date(currentTime);\n   ZonedDateTime zonedTime = time.toInstant().atZone(timeZone.toZoneId());\t\t\n   timeLabel.setText(DateTimeFormatter.ofPattern(\"HH:mm:ss\", Locale.getDefault()).format(zonedTime));\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the view closes (from the red circle\/cross button), it needs to unsubscribe from the publisher, using the <code>subscription.cancel()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void close() {\n   subscription.cancel();\n   Container parent = getParent();\n   parent.remove(this);\n   parent.revalidate();\n   parent.repaint();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can get the rest of the details from the linked GitHub repository. This was a very small demo, just to show how the pub-sub works with the Java interfaces and classes. If you are interested in the details, head to the GitHub demo, clone it and check it out in action!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the following posts I&#8217;ll show some other demonstrations and also write about the updates to the automatic DSA course testing\/checking tool. Have a nice day!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, another long break in blogging. This, of course, is due to having too much work and spending the remaining energy somewhere else (like running). So I thought I&#8217;d write a couple of posts on what I have been doing in between the previous post in January: So, lots of work and thus no time &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2025\/05\/06\/long-break-status-update-demo\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Long break, status update &amp; demo&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[159,77,157,158,70,156,12],"class_list":["post-1153","post","type-post","status-publish","format-standard","hentry","category-coding","tag-demo","tag-java","tag-model-view-controller","tag-mvc","tag-programming","tag-publish-subscribe","tag-teaching"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=1153"}],"version-history":[{"count":6,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1153\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1153\/revisions\/1165"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=1153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=1153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=1153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}