投稿時間:2022-08-28 00:11:24 RSSフィード2022-08-28 00:00 分まとめ(12件)
カテゴリー等 | サイト名等 | 記事タイトル・トレンドワード等 | リンクURL | 頻出ワード・要約等/検索ボリューム | 登録日 |
---|---|---|---|---|---|
python | Pythonタグが付けられた新着投稿 - Qiita | 【OpenCV】Dense Optical Flowを矢印や色空間で表示する方法 | https://qiita.com/yusuke_s_yusuke/items/03243490b1fd765fe61f | denseopticalflow | 2022-08-27 23:23:52 |
python | Pythonタグが付けられた新着投稿 - Qiita | HerokuにヘッドレスブラウザSplashをDockerデプロイする | https://qiita.com/itoi10/items/d08a1de33e6d72f59d51 | docker | 2022-08-27 23:03:34 |
AWS | AWSタグが付けられた新着投稿 - Qiita | CloudFormation で変更なしの変更セットを流したい時 | https://qiita.com/ctakenabe/items/972076e4f746e07e57fd | cloudformation | 2022-08-27 23:12:16 |
Docker | dockerタグが付けられた新着投稿 - Qiita | HerokuにヘッドレスブラウザSplashをDockerデプロイする | https://qiita.com/itoi10/items/d08a1de33e6d72f59d51 | docker | 2022-08-27 23:03:34 |
Git | Gitタグが付けられた新着投稿 - Qiita | bitbucketのレポジトリのコードをbuildしたときにつまづいた | https://qiita.com/greenteabiscuit/items/b164eed7b9d5f62cbdad | lpromptsdisabledconfirm | 2022-08-27 23:33:29 |
技術ブログ | Developers.IO | Amazon QuickSightで利用する地理空間情報データにはどのような地理情報カラムが必要であるか確認してみた | https://dev.classmethod.jp/articles/the-conditions-for-using-geographic-information-data-with-amazon-quicksight/ | creatingm | 2022-08-27 14:55:05 |
海外TECH | MakeUseOf | How to Change What the Fn Keys Do in Windows 10 and 11 | https://www.makeuseof.com/windows-change-fn-keys/ | function | 2022-08-27 14:15:14 |
海外TECH | DEV Community | What the Heck Is Project Loom for Java? | https://dev.to/oktadev/what-the-heck-is-project-loom-for-java-21hh | What the Heck Is Project Loom for Java Java has had good multi threading and concurrency capabilities from early on in its evolution and can effectively utilize multi threaded and multi core CPUs Java Development Kit JDK had basic support for platform threads or Operating System OS threads and JDK had more utilities and updates to improve concurrency and multi threading JDK brought asynchronous programming support and more concurrency improvements While things have continued to improve over multiple versions there has been nothing groundbreaking in Java for the last three decades apart from support for concurrency and multi threading using OS threads Though the concurrency model in Java is powerful and flexible as a feature it was not the easiest to use and the developer experience hasn t been great This is primarily due to the shared state concurrency model used by default One has to resort to synchronizing threads to avoid issues like data races and thread blocking I wrote more about Java concurrency in my Concurrency in modern programming languages Java post What is Project Loom Project Loom aims to drastically reduce the effort of writing maintaining and observing high throughput concurrent applications that make the best use of available hardware ーRon Pressler Tech lead Project Loom OS threads are at the core of Java s concurrency model and have a very mature ecosystem around them but they also come with some drawbacks and are expensive computationally Let s look at the two most common use cases for concurrency and the drawbacks of the current Java concurrency model in these cases One of the most common concurrency use cases is serving requests over the wire using a server For this the preferred approach is the thread per request model where a separate thread handles each request Throughput of such systems can be explained using Little s law which states that in a stable system the average concurrency number of requests concurrently processed by the server L is equal to the throughput average rate of requests λ times the latency average duration of processing each request W With this you can derive that throughput equals average concurrency divided by latency λ L W So in a thread per request model the throughput will be limited by the number of OS threads available which depends on the number of physical cores threads available on the hardware To work around this you have to use shared thread pools or asynchronous concurrency both of which have their drawbacks Thread pools have many limitations like thread leaking deadlocks resource thrashing etc Asynchronous concurrency means you must adapt to a more complex programming style and handle data races carefully There are also chances for memory leaks thread locking etc Another common use case is parallel processing or multi threading where you might split a task into subtasks across multiple threads Here you have to write solutions to avoid data corruption and data races In some cases you must also ensure thread synchronization when executing a parallel task distributed over multiple threads The implementation becomes even more fragile and puts a lot more responsibility on the developer to ensure there are no issues like thread leaks and cancellation delays Project Loom aims to fix these issues in the current concurrency model by introducing two new features virtual threads and structured concurrency Virtual threadsJava is scheduled to be released in September and Virtual threads will be a preview feature Yayyy Virtual threads are lightweight threads that are not tied to OS threads but are managed by the JVM They are suitable for thread per request programming styles without having the limitations of OS threads You can create millions of virtual threads without affecting throughput This is quite similar to coroutines like goroutines made famous by the Go programming language Golang The new virtual threads in Java will be pretty easy to use Compare the below with Golang s goroutines or Kotlin s coroutines Virtual threadThread startVirtualThread gt System out println Hello Project Loom Goroutinego func println Hello Goroutines Kotlin coroutinerunBlocking launch println Hello Kotlin coroutines Fun fact before JDK Java had support for green threads aka virtual threads but the feature was removed in JDK as that implementation was not any better than platform threads The new implementation of virtual threads is done in the JVM where it maps multiple virtual threads to one or more OS threads and the developer can use virtual threads or platform threads as per their needs A few other important aspects of this implementation of virtual threads It is a Thread in code runtime debugger and profilerIt s a Java entity and not a wrapper around a native threadCreating and blocking them are cheap operationsThey should not be pooledVirtual threads use a work stealing ForkJoinPool schedulerPluggable schedulers can be used for asynchronous programmingA virtual thread will have its own stack memoryThe virtual threads API is very similar to platform threads and hence easier to adopt migrateLet s look at some examples that show the power of virtual threads Total number of threadsFirst let s see how many platform threads vs virtual threads we can create on a machine My machine is Intel Core i H with cores threads and GB RAM running Fedora Platform threadsvar counter new AtomicInteger while true new Thread gt int count counter incrementAndGet System out println Thread count count LockSupport park start On my machine the code crashed after platform threads Virtual threadsvar counter new AtomicInteger while true Thread startVirtualThread gt int count counter incrementAndGet System out println Thread count count LockSupport park On my machine the process hung after virtual threads but didn t crash and as memory became available it kept going slowly Task throughputLet s try to run tasks using platform threads try var executor Executors newThreadPerTaskExecutor Executors defaultThreadFactory IntStream range forEach i gt executor submit gt Thread sleep Duration ofSeconds System out println i return i This uses the newThreadPerTaskExecutor with the default thread factory and thus uses a thread group When I ran this code and timed it I got the numbers shown here I get better performance when I use a thread pool with Executors newCachedThreadPool newThreadPerTaskExecutor with defaultThreadFactory real s user s sys pu amem mmem newCachedThreadPool with defaultThreadFactory real s user s sys pu amem mmemNot so bad Now let s do the same using virtual threads try var executor Executors newVirtualThreadPerTaskExecutor IntStream range forEach i gt executor submit gt Thread sleep Duration ofSeconds System out println i return i If I run and time it I get the following numbers real s user s sys pu amem mmemThis is far more performant than using platform threads with thread pools Of course these are simple use cases both thread pools and virtual thread implementations can be further optimized for better performance but that s not the point of this post Running Java Microbenchmark Harness JMH with the same code gives the following results and you can see that virtual threads outperform platform threads by a huge margin ThroughputBenchmark Mode Cnt Score Error UnitsLoomBenchmark platformThreadPerTask thrpt ± ops sLoomBenchmark platformThreadPool thrpt ± ops sLoomBenchmark virtualThreadPerTask thrpt ± ops s Average timeBenchmark Mode Cnt Score Error UnitsLoomBenchmark platformThreadPerTask avgt ± s opLoomBenchmark platformThreadPool avgt ± s opLoomBenchmark virtualThreadPerTask avgt ± s opYou can find the benchmark source code on GitHub Here are some other meaningful benchmarks for virtual threads An interesting benchmark using ApacheBench on GitHub by Elliot BarlasA benchmark using Akka actors on Medium by Alexander ZakusyloJMH benchmarks for I O and non I O tasks on GitHub by Colin Cachia Structured concurrencyStructured concurrency will be an incubator feature in Java Structured concurrency aims to simplify multi threaded and parallel programming It treats multiple tasks running in different threads as a single unit of work streamlining error handling and cancellation while improving reliability and observability This helps to avoid issues like thread leaking and cancellation delays Being an incubator feature this might go through further changes during stabilization Consider the following example using java util concurrent ExecutorService void handleOrder throws ExecutionException InterruptedException try var esvc new ScheduledThreadPoolExecutor Future lt Integer gt inventory esvc submit gt updateInventory Future lt Integer gt order esvc submit gt updateOrder int theInventory inventory get Join updateInventory int theOrder order get Join updateOrder System out println Inventory theInventory updated for order theOrder We want updateInventory and updateOrder subtasks to be executed concurrently Each of those can succeed or fail independently Ideally the handleOrder method should fail if any subtask fails However if a failure occurs in one subtask things get messy Imagine that updateInventory fails and throws an exception Then the handleOrder method throws an exception when calling inventory get So far this is fine but what about updateOrder Since it runs on its own thread it can complete successfully But now we have an issue with a mismatch in inventory and order Suppose the updateOrder is an expensive operation In that case we are just wasting the resources for nothing and we will have to write some sort of guard logic to revert the updates done to order as our overall operation has failed Imagine that updateInventory is an expensive long running operation and updateOrder throws an error The handleOrder task will be blocked on inventory get even though updateOrder threw an error Ideally we would like the handleOrder task to cancel updateInventory when a failure occurs in updateOrder so that we are not wasting time If the thread executing handleOrder is interrupted the interruption is not propagated to the subtasks In this case updateInventory and updateOrder will leak and continue to run in the background For these situations we would have to carefully write workarounds and failsafe putting all the burden on the developer We can achieve the same functionality with structured concurrency using the code below void handleOrder throws ExecutionException InterruptedException try var scope new StructuredTaskScope ShutdownOnFailure Future lt Integer gt inventory scope fork gt updateInventory Future lt Integer gt order scope fork gt updateOrder scope join Join both forks scope throwIfFailed and propagate errors Here both forks have succeeded so compose their results System out println Inventory inventory resultNow updated for order order resultNow Unlike the previous sample using ExecutorService we can now use StructuredTaskScope to achieve the same result while confining the lifetimes of the subtasks to the lexical scope in this case the body of the try with resources statement The code is much more readable and the intent is also clear StructuredTaskScope also ensures the following behavior automatically Error handling with short circuiting ーIf either the updateInventory or updateOrder fails the other is canceled unless its already completed This is managed by the cancellation policy implemented by ShutdownOnFailure other policies are possible Cancellation propagation ーIf the thread running handleOrder is interrupted before or during the call to join both forks are canceled automatically when the thread exits the scope Observability ーA thread dump would clearly display the task hierarchy with the threads running updateInventory and updateOrder shown as children of the scope State of Project LoomThe Loom project started in and has undergone many changes and proposals Virtual threads were initially called fibers but later on they were renamed to avoid confusion Today with Java getting closer to release the project has delivered the two features discussed above One as a preview and another as an incubator Hence the path to stabilization of the features should be more precise What does this mean to regular Java developers When these features are production ready it should not affect regular Java developers much as these developers may be using libraries for concurrency use cases But it can be a big deal in those rare scenarios where you are doing a lot of multi threading without using libraries Virtual threads could be a no brainer replacement for all use cases where you use thread pools today This will increase performance and scalability in most cases based on the benchmarks out there Structured concurrency can help simplify the multi threading or parallel processing use cases and make them less fragile and more maintainable What does this mean to Java library developers When these features are production ready it will be a big deal for libraries and frameworks that use threads or parallelism Library authors will see huge performance and scalability improvements while simplifying the codebase and making it more maintainable Most Java projects using thread pools and platform threads will benefit from switching to virtual threads Candidates include Java server software like Tomcat Undertow and Netty and web frameworks like Spring and Micronaut I expect most Java web technologies to migrate to virtual threads from thread pools Java web technologies and trendy reactive programming libraries like RxJava and Akka could also use structured concurrency effectively This doesn t mean that virtual threads will be the one solution for all there will still be use cases and benefits for asynchronous and reactive programming If you like this article please leave a like or a comment You can follow me on Twitter and LinkedIn The cover image was created using a photo by Peter Herrmann on UnsplashThis post was originally published on the Okta Developer Blog | 2022-08-27 14:29:25 |
北海道 | 北海道新聞 | 「ユウダイのおかげ」「ファンの応援で安心」 無安打無得点のポンセ、周囲への感謝忘れない愛されキャラ | https://www.hokkaido-np.co.jp/article/722783/ | 無安打無得点 | 2022-08-27 23:24:50 |
北海道 | 北海道新聞 | 維新、新代表に馬場伸幸氏 松井路線継承、共同代表に吉村洋文氏 | https://www.hokkaido-np.co.jp/article/722760/ | 吉村洋文 | 2022-08-27 23:21:07 |
北海道 | 北海道新聞 | ニジイロクワガタに夢中 帯広で「世界の大昆虫展」 | https://www.hokkaido-np.co.jp/article/722785/ | 大昆虫展 | 2022-08-27 23:11:48 |
北海道 | 北海道新聞 | 「小説」描き被災を想像 釧路・景雲中、釧教大教授招き授業 | https://www.hokkaido-np.co.jp/article/722779/ | 巨大地震 | 2022-08-27 23:09:53 |
コメント
コメントを投稿