我正在使用ning async http客户端来实现非阻塞性的优点.做一个苹果对苹果测试(非阻塞与阻塞),我发现非阻塞版本正在提供更多的请求样本,但异步http客户端正在创建比其阻塞对应物更多的线程.这是预期还是我遗失的东西?
以下是压力测试的数字
Async Http Client
jMeter - 2 threads, 120 seconds, 1 sec ramp up
Peak threads : 270
Heap usage: 600mb
Peak cpu usage: 30%
Total samples: 18228
Blocking version
jMeter - 2 threads, 120 seconds, 1 sec ramp up
Peak threads: 118
heap usage: 260mb
cpu usage: 18%
total samples: 1472
Run Code Online (Sandbox Code Playgroud)
我正在创建一个连接线程池(重用它们)
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
builder.setRequestTimeoutInMs(2000);
builder.setMaximumConnectionsPerHost(10);
builder.setMaximumConnectionsTotal(100);
client = new AsyncHttpClient(builder.build());
Run Code Online (Sandbox Code Playgroud)
这里有什么我想念的吗?我试着查看线程转储以查看创建线程的内容,但没有找到任何有用的东西.我敢打赌,生成的每个http连接都有一个线程,用于在异步http客户端的I/O完成时触发回调.
我正在寻找一种简化正则表达式的方法,该正则表达式由值(例如12345),关系符号(<,>,<=,> =)和junctors(&,!)组成.例如表达式:
>= 12345 & <=99999 & !55555
Run Code Online (Sandbox Code Playgroud)
应该匹配.我有这个正则表达式:
(^<=|^<= | ^>= | ^>= |^<|^>|^< |^> |^)((!|)([0-9]{1,5}))( & > | & < |& >=|&>=|&<=||&<=|&>=|&<|&>|&| &| & |$))*
Run Code Online (Sandbox Code Playgroud)
我特别不满意在表达式的开头和结尾重复<=,> =,<,>.我很乐意得到一个提示如何使其变得更简单,例如向前看,回顾一下.
我想按出生年份的降序排序我的数组.我的数组有另外两个String类型的元素.因此,作为一个例子,出生在最早年的人,如1939年,将处于最顶层,然后如此.
这是我的代码:
import java.util.*;
public class StudentInformationTest
{
public static void main (String [] args){
StudentInformation[] studentInfo = new StudentInformation[10];
studentInfo[0] = new StudentInformation("Student A",1971, "BSc FDIT");
studentInfo[1] = new StudentInformation("Student B",1964, "BSc FDIT");
studentInfo[2] = new StudentInformation("Student C",1996, "BSc FDIT");
studentInfo[3] = new StudentInformation("Student D",1939, "BSc FDIT");
studentInfo[4] = new StudentInformation("Student E",1945, "BSc FDIT");
studentInfo[5] = new StudentInformation("Student F",1991, "BSc FDIT");
studentInfo[6] = new StudentInformation("Student G",1987, "BSc FDIT");
studentInfo[7] = new StudentInformation("Student H",1968, "BSc FDIT");
studentInfo[8] = new StudentInformation("Student …Run Code Online (Sandbox Code Playgroud) 假设在我的代码中某处我写了一个空synchronized块:
synchronized(obj){
//No code here
}
Run Code Online (Sandbox Code Playgroud)
因为同步块不包含任何代码,JIT编译器obj是否会通过不锁定来优化它,因为它没有用?
Java编译器做类似的技巧,如Lock粗化,但是这个同步块也会被优化掉吗?
编辑:
根据assylias的观点,
synchronized(new Object()){
//empty block
}
Run Code Online (Sandbox Code Playgroud)
JIT编译器现在能够优化它,因为我使用的是一个不会逃避我的方法的Object吗?
我在FreeRTOS源代码中遇到过这篇文章:
void vApplicationIdleHook( void )
{
/* The simple blinky demo does not use the idle hook - the full demo does. */
#if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
{
extern void vFullDemoIdleHook( void );
//* Implemented in main_full.c. */
vFullDemoIdleHook();
}
#endif
}
Run Code Online (Sandbox Code Playgroud)
为什么要声明这样的函数/方法?有什么好处?我在Java中也看到了类似的代码.
在回答问题时,我遇到了Java类型推断的这种奇怪行为.以下工作没有问题:
new HashMap<String,Integer>().entrySet().stream()
.sorted(Map.Entry.comparingByValue());
Run Code Online (Sandbox Code Playgroud)
但如果我们.reversed()最后添加,它会突然失败:
new HashMap<String,Integer>().entrySet().stream()
.sorted(Map.Entry.comparingByValue().reversed());
Run Code Online (Sandbox Code Playgroud)
细节:在第一种情况下,comparingByValue()推断类型为Comparator<Map.Entry<String, Integer>>,但在第二种情况下,它更改为Comparator<Map.Entry<Object, V>>.
类型reversed()只是Comparator<T>对其目标类型的身份转换.为什么类型推断会在这样一个看似微不足道的步骤中失去它?我无法摆脱预感,这必定是由于推断机制中的一个错误.
作为进一步的难题,如果我们只是内联实现reversed(),那就是return Collections.reverseOrder(this);:
new HashMap<String,Integer>().entrySet().stream()
.sorted(reverseOrder(Map.Entry.comparingByValue());
Run Code Online (Sandbox Code Playgroud)
它又有效了.
我已经声明了ArrayList,
var otherSeriesList = ArrayList<String>()
Run Code Online (Sandbox Code Playgroud)
并尝试通过以下方式从资源获取数据,
otherSeriesList = ArrayList<String>(Arrays.asList(resources.getStringArray(R.array.get_other_series)))
Run Code Online (Sandbox Code Playgroud)
我应该如何从资源字符串数组创建ArrayList?
我试图在0.30.0中使用最新的协同程序,并且无法弄清楚如何使用新的作用域.在原始的协同程序中,我可以使用UI或CommonPool设置上下文,一切正常.
现在我正在尝试在从房间数据库中读取时在我的ViewModel中使用GlobalScope,然后我想将返回的值赋给我的LiveData对象.
我尝试设置LiveData值时收到以下错误
java.lang.IllegalStateException:无法在后台线程上调用setValue
fun getContact() {
GlobalScope.launch {
val contact = contacts.getContact() // suspended function
withContext(Dispatchers.Default) { phoneContact.value = contact }
}
}
Run Code Online (Sandbox Code Playgroud)
我只看到调度员的默认,无限和IO,而且没有一个工作,我无法弄清楚我做错了什么?我对主线程的选择在哪里?
我在Kotlin项目中使用MVP模式.我有一个Presenter类:
import com.google.gson.Gson
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import org.jetbrains.anko.coroutines.experimental.bg
class TeamsPresenter(private val view: TeamsView,
private val apiRepository: ApiRepository,
private val gson: Gson
) {
fun getTeamList(league: String?) {
view.showLoading()
async(UI){
val data = bg {
gson.fromJson(apiRepository
.doRequest(TheSportDBApi.getTeams(league)),
TeamResponse::class.java
)
}
view.showTeamList(data.await().teams)
view.hideLoading()
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个演示者课程在Kotlin 1.2.71上正常工作,但我无法在Kotlin 1.3.0上工作.
我在项目的build.gradle中更新了Kotlin版本,删除了"实验协同程序"并添加了kotlin coroutine core依赖:
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码:
import com.google.gson.Gson
class TeamsPresenter(private val view: TeamsView,
private val apiRepository: ApiRepository,
private val gson: Gson
) {
fun getTeamList(league: String?) {
view.showLoading()
async(UI){
val data = …Run Code Online (Sandbox Code Playgroud) 当我在视图模型中添加协程延迟()时,代码的其余部分将不会被执行。
这是我的演示代码:
class SimpleViewModel : ViewModel(), CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Unconfined
var data = 0
fun doSomething() {
launch {
delay(1000)
data = 1
}
}
}
class ScopedViewModelTest {
@Test
fun coroutineDelay() {
// Arrange
val viewModel = SimpleViewModel()
// ActTes
viewModel.doSomething()
// Assert
Assert.assertEquals(1, viewModel.data)
}
}
Run Code Online (Sandbox Code Playgroud)
我得到断言结果:
java.lang.AssertionError:
Expected :1
Actual :0
Run Code Online (Sandbox Code Playgroud)
知道如何解决这个问题吗?