我发现了这篇SO帖子,建议在有向图中使用DFS进行循环检测由于回溯而更快.在这里我引用该链接:
深度优先搜索比广度优先搜索更有效,因为您可以更快地回溯.如果使用调用堆栈,它也更容易实现,但这依赖于没有溢出堆栈的最长路径.
此外,如果您的图表是定向的,那么您不仅要记住您是否访问过某个节点,还要记得您是如何到达该节点的.否则你可能会认为你找到了一个循环,但实际上你所拥有的是两个独立的路径A-> B,但这并不意味着存在路径B-> A. 通过深度优先搜索,您可以在下降时将节点标记为已访问,并在您回溯时将其标记为未标记.
为什么回溯必不可少?
有人可以用示例图解释给定A->B示例中的含义吗?
最后,我DFS在有向图中有一个循环检测代码,它不使用回溯但仍然检测循环O(E+V).
public boolean isCyclicDirected(Vertex v){
if (v.isVisited) {
return true;
}
v.setVisited(true);
Iterator<Edge> e = v.adj.iterator();
while (e.hasNext()) {
Vertex t = e.next().target;
// quick test:
if (t.isVisited) {
return true;
}
// elaborate, recursive test:
if (isCyclicDirected(t)) {
return true;
}
}
// none of our adjacent vertices flag as cyclic
v.setVisited(false);
return false;
}
Run Code Online (Sandbox Code Playgroud) algorithm directed-graph backtracking cyclic depth-first-search
在我的小小理解中,multi-threading在大多数情况下驱动编程的性能因素并非全部.(不论Java或Python).
我读这启发性的文章上GIL的SO.文章总结了python采用的GIL机制; 即只有一个single Thread可以python byte code在任何给定时间执行.这使得single thread应用程序真的更快.
我的问题如下:
因为如果Thread在给定点只提供一个,是否multiprocessing或thread模块提供了克服GIL强加的限制的方法?如果没有,它们为实际multi-task工作提供了哪些功能
在接受的答案中,上述帖子的评论部分提出了一个问题,但没有回答?我脑子里也有这个问题
^so at any time point of time, only one thread will be serving content to client...
so no point of actually using multithreading to improve performance. right?
Run Code Online (Sandbox Code Playgroud) 在做教程的同时Cassandra,讲师提到cassandra没有single point of failure.也就是说,如果节点在群集中关闭,则不应影响客户端访问它的方式.
所以我尝试了以下操作:关闭我的3节点集群中的一个节点.
然后尝试连接到已关闭的节点.
我被拒绝连接很明显,因为该节点已关闭.但正如教练提到的那样,我期待cassandra以循环方式将连接路由到下一个节点.但事实并非如此.这是为什么?
ccm status
node1: UP
node3: UP
node2: UP
ccm node2 stop
ccm status
node1: UP
node3: UP
node2: DOWN
ccm node2 cqlsh
Connection error: Could not connect to 127.0.0.2:9160
Run Code Online (Sandbox Code Playgroud)
编辑:
我注意到的另一件事是我能够进行写操作.但默认操作失败.我没有调整一致性水平.我正在使用默认值.我得到的是:
cqlsh> CREATE KEYSPACE example with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
cqlsh> use example ;
cqlsh:example> CREATE TABLE users(id varchar PRIMARY KEY, email varchar, password varchar, name varchar);
cqlsh:example> INSERT INTO users (id, email, name, …Run Code Online (Sandbox Code Playgroud) 我正在阅读关于OAuth2如何工作的博客.这是一个很好的来源,我想我已经理解了OAuth2如何工作的基础知识.
在阅读时Authorization grant,涉及授予对应用服务器(我的服务器)的访问权限,该服务器通过注册的重定向URI交换从授权服务器(例如,facebook)接收的授权代码.
myserver然后交换此access_token和刷新令牌的授权码.当访问令牌过期时,刷新令牌用于获取新的访问令牌.
Q1)从这个流程中,我看到facebook提供的授权代码只在我的服务器上使用一次才能获得access_token.对于后续请求,不使用此授权代码.它是否正确 ?
如果用户在访问令牌过期的3天后登录我的服务器,我的服务器将使用刷新令牌获取新的访问令牌并使用此访问令牌.
Q2)刷新令牌是否过期或每次使用刷新令牌获取新的访问令牌时,是否提供了新的刷新令牌?
我们目前使用的是 Java EE 5,我们在发送响应之前执行以下操作将 POJO 转换为 JSON。
@GET
@Path("/books")
@Produces(MediaType.APPLICATION_JSON)
public Response getBooks() {
List<Book> listOfBooks = getMiscService().getbooks();
String response = "{\"books\":" + gson.toJson(listOfBooks) + "}";
return Response.status(Response.Status.OK).entity(response).build();
}
Run Code Online (Sandbox Code Playgroud)
我们正在使用谷歌的 gson API。现在我们正在将代码重组为符合 Java EE 7 API 的,我想知道是否有任何 JSON 转换 API 可以将 POJO 转换为 JSON。
我知道 Java EE 7 中引入的 JsonObject API。但我仍然想知道如何获得我的 POJO 的 JSON 表示。
JsonObject jsonObject = Json.createObjectBuilder().add("books", myObject);
Run Code Online (Sandbox Code Playgroud)
上面的 myObject 需要是我的对象的 JSON 表示正确吗?
我正在考虑这个。但这仍然使用 Gson
JsonObject jsonObject = Json.createObjectBuilder().add("books", gson.toJson(myObject));
Run Code Online (Sandbox Code Playgroud)
这里推荐的方式是什么?
谢谢
这可能没有直接用例.但我想知道为什么以下被认为是无效的json
{"hello":
{[1,2,3,4] : "foobar" }
}
Run Code Online (Sandbox Code Playgroud) public class myService {
@Autowired
ExecutorService executor;
public Result getServiceResult(Token token, Profile profile){
//validate token and profile
return getResult(token, profile).get();
}
private getResult (Token token, Profile profile){
Future<Result> = threadPoolExecutor.submit(
() -> myManager.createAccount(token, profile));
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码在我目前的工作中运行良好.我无法理解如何threadPoolExecutor.submit通过"功能/方法"但不能通过callable?
我正在使用Java 8和Spring框架.
这是我的控制器类:
@Controller
@RequestMapping("/actuator")
public class HealthController {
@RequestMapping(value = "/metrics", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
@ResponseBody
public HealthModel getDump() throws JsonProcessingException {
return new HealthModel();
//return mapper.writeValueAsString(metrics.invoke());
}
@RequestMapping(value = "/metrics", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN)
@ResponseBody
public String getHealth() {
return "HEALTHY";
}
}
Run Code Online (Sandbox Code Playgroud)
模型
public class HealthModel {
@JsonProperty
private String status;
@JsonProperty
private int id;
public HealthModel(){
this.status="WARN";
this.id=2;
}
}
Run Code Online (Sandbox Code Playgroud)
注意我已经映射/metrics到返回json或plain-text取决于Accept Header请求中的
当我提出请求时
curl -v -H …
我很难理解一个简单的问题regex.我用Google搜索了一下.不知何故,它并没有让我感到惊讶.
这是方法:
public static void testMethod(){
String line = "This order was placed for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
System.out.println("Found value: " + m.group(3) );
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
我原本group(2)打算打印3000.但为什么它只打印0.
我有一个 Spring Boot gradle 应用程序。当我运行 gradle 构建时,我想在不同的环境中运行。
例如:./gradlew clean build -Dapp.env=QA1。在测试代码中,我想检查此属性并相应地收集测试数据。我观察到的是该属性 (app.env) 不可用。
由于测试会检查系统属性,因此构建应该会失败。但构建成功。我也没有在控制台中看到 println 语句。
如果你想克隆回购:
git 克隆https://SpringDevSeattle@bitbucket.org/SpringDevSeattle/gs-rest-service.git
这是我的测试代码:
src/test/java/hello/GreetingControllerTests.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ContextConfiguration(classes={hello.TestConfig.class,hello.AppConfig.class})
@WebAppConfiguration
public class GreetingControllerTests {
@Autowired
TestEnv testEnv;
@Autowired
Status status;
private String env;
@Before
public void init(){
System.out.println(status.getName());
env=testEnv.getEnv();
}
@Test
public void should_fail(){
if (env.equalsIgnoreCase("DEV")){
assertThat(false).isFalse();
}
if (env.equalsIgnoreCase("QA1")){
System.out.println("failing the test");
fail();
}
}
}
Run Code Online (Sandbox Code Playgroud)
src/test/java/hello/TestConfig.java
@Configuration
public class TestConfig {
@Bean
public TestEnv testEnv(){
Properties properties = …Run Code Online (Sandbox Code Playgroud) json ×2
spring ×2
algorithm ×1
arrays ×1
backtracking ×1
callable ×1
cassandra ×1
cyclic ×1
facebook ×1
future ×1
gil ×1
gradle ×1
gson ×1
jakarta-ee ×1
java ×1
java-8 ×1
java-ee-7 ×1
jsonobject ×1
oauth ×1
oauth-2.0 ×1
python ×1
regex ×1
spring-boot ×1
spring-mvc ×1
spring-rest ×1
spring-test ×1