Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Java development based on role model?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how Java development based on role model is, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Debugging indefinite defects in multithreaded applications is the most painful task for software developers. So, like most people, I love to use functional languages such as Erlang and Scala for concurrent programming.

Both Scala and Erlang use a role model for concurrent programming, without the concept of threads. The innovation around the role model is not limited to the language itself, but can also be used by Java-based role frameworks such as Kilim.

Kilim's use of the role model is very intuitive, and as you'll see later, the library makes it extremely easy to build concurrent applications.

Multicore challenge

In 2005, Herb Sutter wrote the still famous article "The Free Lunch is Over: A Fundamental Turn Toward Concurrency in Software". In this article, he rejects the misleading belief that Moore's Law will continue to promote higher and higher CPU clock rates.

Sutter predicts the end of "free lunch", and it will no longer be possible to improve the performance of software applications with faster and faster chips. Instead, he believes that a significant improvement in application performance will need to be achieved using a multi-core chip architecture.

He turned out to be right. Chipmakers have reached a hard limit, with chip speeds stable at around 3.5 GHz for many years. As manufacturers increase the number of cores on chips more and more quickly, Moore's Law continues to be satisfied in the multi-core field.

Sutter also mentioned that concurrent programming will enable developers to take advantage of multi-core architectures. However, he added, "We urgently need a more advanced concurrent programming model than the programming models provided by today's various languages."

The basic programming model of languages such as Java is thread-based. Although multithreaded applications are not difficult to write, there are still many challenges in writing them correctly. One of the difficulties of concurrent programming is to use threads to consider concurrency. There are many concurrency models today, and one that is particularly interesting and accepted by the Java community is the role model.

Role model

Role model is a different way of modeling concurrent processes. Unlike threads that interact with locks through shared memory, the role model takes advantage of the concept of "role" and uses mailboxes to deliver asynchronous messages. Here, mailboxes are similar to real-life mailboxes, where messages can be stored and retrieved by other roles for processing. Mailboxes effectively separate processes from each other without sharing variables in memory.

Roles act as separate and completely different entities and do not share memory to communicate. In fact, roles can only communicate through mailboxes. There are no locks and synchronization blocks in the role model, so there are no problems caused by them, such as deadlocks and serious lost updates. Moreover, roles can work concurrently, rather than in some sequential way. As a result, roles are more secure (no locks and synchronization are required), and the role model itself can handle coordination issues. In essence, the role model makes concurrent programming easier.

Role model is not a new concept, it has been around for a long time. The concurrency models of some languages, such as Erlang and Scala, are role-based rather than thread-based. In fact, Erlang's success in the enterprise environment (Erlang was founded by Ericsson and has a long history in telecommunications) undoubtedly makes the role model more popular, more exposed, and makes it a viable choice for other languages. Erlang is an excellent example of a more secure concurrent programming approach to the role model.

Unfortunately, the role model is not embedded in the Java platform, but we can use it in a variety of ways. JVM's openness to alternative languages means that you can leverage roles through Java platform languages such as Scala or Groovy (see Resources for Groovy's role library GPars). In addition, you can try a Java-based library that supports a role model, such as Kilim.

Roles in Kilim

Kilim is a library written in Java that incorporates the concept of a role model. In Kilim, "roles" are represented using the Task type of Kilim. Task are lightweight threads that communicate with other Task through Kilim's Mailbox type.

Mailbox can accept any type of "message". For example, the Mailbox type accepts java.lang.Object. Task can send String messages or even custom message types, depending on you.

In Kilim, all entities are bundled together by method signatures, and if you need to perform several operations at the same time, you can specify this behavior in a method and expand the method's signature to throw a Pausable. Therefore, creating concurrent classes in Kilim is as simple as implementing Runnable or extending Thread in Java. It's just that there are fewer additional entities that use Runnable or Thread, such as the keyword synchronized.

* the magic of Kilim is realized by a late process called weaver, which converts the bytecode of the class. Methods that contain Pausable throws words are handled at run time by a scheduler that is included in the Kilim library. The scheduler handles a limited number of kernel threads. You can use this tool to handle more lightweight threads, which can increase the speed of context switching and startup to a limited extent. The stack of each thread is managed automatically.

In essence, Kilim makes it easy and easy to create concurrent processes: simply extend from the Task type of Kilim and implement the execute method. After compiling the newly created class that supports concurrency, you will achieve a significant performance improvement by running Kilim's weaver on it!

Kilim was originally a foreign language, but it brought huge rewards. The role model (and later Kilim) makes it easier and safer to write asynchronous operands that depend on similar objects. You can do the same using Java's basic threading model (such as extending Thread), but this is more challenging because it brings you back to the world of locking and synchronization. In short, converting your concurrent programming model to roles makes multithreaded applications easier to code.

On the role-based model of Java development is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report